简体   繁体   English

根据另一列中的值对 R dataframe 中的列进行分组

[英]group columns in R dataframe based on values in another column

Consider the following pair of lists考虑以下一对列表

 ID<-c("A", "B")
 Var2<-c("T_X", "X_F", "R_X", "T_Y", "Y_F", "R_Y")

I have created the following dataframe我创建了以下 dataframe

df2<-expand.grid(ID, Var2)

The resultant dataframe is as follows得到的 dataframe 如下

   Var1 Var2
1     A  T_X
2     B  T_X
3     A  X_F
4     B  X_F
5     A  R_X
6     B  R_X
7     A  T_Y
8     B  T_Y
9     A  Y_F
10    B  Y_F
11    A  R_Y
12    B  R_Y

I would like to reorder the dataframe by Var1 column so that all values corresponding to A are together and likewise with B. (Note this is a toy dataset and the real number of unique values in Var1 can be much higher).我想按 Var1 列对 dataframe 重新排序,以便与 A 对应的所有值都在一起,并且与 B 一样。(注意这是一个玩具数据集,Var1 中唯一值的实际数量可能要高得多)。

The following is the expected output以下是预期的output

   Var1 Var2
1     A  T_X
3     A  X_F
5     A  R_X
7     A  T_Y
9     A  Y_F
11    A  R_Y
2     B  T_X
4     B  X_F
6     B  R_X
8     B  T_Y
10    B  Y_F
12    B  R_Y

I have tried df2%>% group_by(Var1).我试过 df2%>% group_by(Var1)。 this has left the dataframe unchanged.这使 dataframe 保持不变。

I request someone to help me here.我请求有人在这里帮助我。

We can do it in reverse.我们可以反过来做。 No need of any order ing afterwards or any packages之后无需任何order或任何包裹

setNames(expand.grid(Var2, ID)[2:1], c("Var1", "Var2"))

-output -输出

#    Var1 Var2
#1     A  T_X
#2     A  X_F
#3     A  R_X
#4     A  T_Y
#5     A  Y_F
#6     A  R_Y
#7     B  T_X
#8     B  X_F
#9     B  R_X
#10    B  T_Y
#11    B  Y_F
#12    B  R_Y

Or use crossing from tidyr或者使用来自tidyrcrossing

library(tidyr)
crossing(ID, Var2 = factor(Var2, levels = Var2))

-output -输出

#   ID    Var2 
#   <chr> <chr>
# 1 A     T_X  
# 2 A     X_F  
# 3 A     R_X  
# 4 A     T_Y  
# 5 A     Y_F  
# 6 A     R_Y  
# 7 B     T_X  
# 8 B     X_F  
# 9 B     R_X  
# 10 B     T_Y  
# 11 B     Y_F  
# 12 B     R_Y  

You can use tidyr 's expand_grid which works as expected here.您可以使用tidyrexpand_grid ,它在此处按预期工作。

tidyr::expand_grid(ID, Var2)

#   ID    Var2 
#   <chr> <chr>
# 1 A     T_X  
# 2 A     X_F  
# 3 A     R_X  
# 4 A     T_Y  
# 5 A     Y_F  
# 6 A     R_Y  
# 7 B     T_X  
# 8 B     X_F  
# 9 B     R_X  
#10 B     T_Y  
#11 B     Y_F  
#12 B     R_Y  

However, you can always order df2 output to get output in required format.但是,您始终可以order df2 output 以获取所需格式的 output。

df2 <- expand.grid(ID, Var2)
df2[order(df2$Var1), ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一个数据框中各列的范围将一个数据框中的值分组 - Group values in one dataframe based on range in columns in another dataframe 使用 R - 根据另一个数据帧的组最大值重塑数据帧 - Using R - reshape a dataframe based on group max values of another dataframe 根据R中另一个数据框中的值重命名数据框列 - Rename dataframe columns based on values in another dataframe in R 基于r中另一个数据帧中的列向数据帧添加值 - Adding values to a dataframe based on columns in another dataframe in r 根据其他数据框中列的值选择 R 数据框中的列 - Selecting columns in R dataframe based on values of column in other dataframe 根据 R 中同一 dataframe 中的另一列的值将值分配给一列 - Assigning values to a column in the based on values of another column in the same dataframe in R 如何根据R中另一列中的值替换数据框的列中的值? - How to replace values in the columns of a dataframe based on the values in the other column in R? 折叠数据框,创建新列,名称是另一列的唯一值,值基于另一列的值? 在 R - Collapse a dataframe, creating new columns with name being the unique values of another column, and value based on the value of another column? In R 如何根据R中的分组将单独的列值添加到另一列? - How to add seperate column values to another column based on group by in R? R:根据数据框另一列中的不同信息合并列 - R: Combine columns based on different information in another column of a dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM