有没有办法连接 R 中除最后两个之外的所有前导列值?
下面是我的数据框的片段
DISEASE Gender Race Freq NEWCOL
Salmonellosis M RACE_LATINO_HISPANIC 1 NA
Salmonellosis F BLACK_AFRICAN_AMERICAN 2 NA
Salmonellosis M WHITE 3 NA
Salmonellosis M WHITE 4 NA
使用concatenate inexcel得到的期望结果
DISEASE Gender Race Freq NEWCOL Concat
Salmonellosis M RACE_LATINO_HISPANIC 1 NA Salmonellosis M RACE_LATINO_HISPANIC
Salmonellosis F BLACK_AFRICAN_AMERICAN 2 NA Salmonellosis F BLACK_AFRICAN_AMERICAN
Salmonellosis M WHITE 3 NA Salmonellosis M WHITE
Salmonellosis M WHITE 4 NA Salmonellosis M WHITE
我尝试在 R 中粘贴,但找不到忽略最后两列的方法
此外,在我的应用程序中,列数会随着每次迭代而变化,因此我需要一个忽略最后两列的函数,而不是选择几个前导列
最佳答案
这不是一个优雅的解决方案,但根据您的数据,您可以简单地使用 apply
并传入您的 data.frame
,最后两列由动态引用列数。
df = readr::read_table2("DISEASE Gender Race Freq NEWCOL
Salmonellosis M RACE_LATINO_HISPANIC 1 NA
Salmonellosis F BLACK_AFRICAN_AMERICAN 2 NA
Salmonellosis M WHITE 3 NA
Salmonellosis M WHITE 4 NA")
df$Concat = apply(df[,1:(ncol(df)-2)],1,paste,collapse=" ")
关于r - 连接 R 中除最后两列以外的所有列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50475592/