Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 按disticnt行值将多行合并到单个列中_R_Dataframe_Data Science - Fatal编程技术网

R 按disticnt行值将多行合并到单个列中

R 按disticnt行值将多行合并到单个列中,r,dataframe,data-science,R,Dataframe,Data Science,使用R尝试合并原始矩阵,以基于行值的值生成矩阵 例: 发件人: 致: 我们可以使用split split(df1[,2], df1[,1]) #$a1 #[1] 10 20 40 #$a2 #[1] 45 50 #$a3 #[1] 40 #$a4 #[1] 45 60 要创建向量的列表或使用聚合: aggregate(df$X2~df$X1, df, rbind) # df$X1 df$X2 # 1 a1 10, 20, 40 # 2 a2 45, 5

使用R尝试合并原始矩阵,以基于行值的值生成矩阵

例:

发件人:

致:


我们可以使用
split

split(df1[,2], df1[,1])
#$a1
#[1] 10 20 40

#$a2
#[1] 45 50

#$a3
#[1] 40

#$a4
#[1] 45 60

要创建
向量的
列表

或使用
聚合

aggregate(df$X2~df$X1, df, rbind)

  # df$X1      df$X2
# 1    a1 10, 20, 40
# 2    a2     45, 50
# 3    a3         40
# 4    a4     45, 60
数据

df <- structure(list(X1 = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 4L, 4L
), .Label = c("a1", "a2", "a3", "a4"), class = "factor"), X2 = c(10L, 
20L, 40L, 45L, 50L, 40L, 45L, 60L)), .Names = c("X1", "X2"), class = 
"data.frame", row.names = c(NA, 
    -8L))
df
aggregate(df$X2~df$X1, df, rbind)

  # df$X1      df$X2
# 1    a1 10, 20, 40
# 2    a2     45, 50
# 3    a3         40
# 4    a4     45, 60
df <- structure(list(X1 = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 4L, 4L
), .Label = c("a1", "a2", "a3", "a4"), class = "factor"), X2 = c(10L, 
20L, 40L, 45L, 50L, 40L, 45L, 60L)), .Names = c("X1", "X2"), class = 
"data.frame", row.names = c(NA, 
    -8L))