[英]Combine contents in different columns into one column
Dear Stack overflow genius,亲爱的堆栈溢出天才,
I am trying to combine a data.frame containing 10 columns into 1 column and then remove duplicate.我正在尝试将包含 10 列的 data.frame 合并为 1 列,然后删除重复项。 Now I am doing it in a stupid way, can anyone show me how to loop it?
现在我正在以一种愚蠢的方式进行操作,有人可以告诉我如何循环吗? Sry I am so new to R.
对不起,我对 R 很陌生。 Thank you so much!
太感谢了!
The data is very simple and here is just an example数据很简单,这里只是一个例子
C1 ![]() |
c2 ![]() |
c3 ![]() |
c4 ![]() |
c5 ![]() |
c6 ![]() |
c7 ![]() |
c8 ![]() |
c9 ![]() |
c10 ![]() |
---|---|---|---|---|---|---|---|---|---|
13 ![]() |
16 ![]() |
19 ![]() |
1 ![]() |
20 ![]() |
36 ![]() |
8 ![]() |
22 ![]() |
10 ![]() |
20 ![]() |
15 ![]() |
20 ![]() |
16 ![]() |
`col1 <- as.character(allcol[,1])
col2 <- as.character(allcol[,2])
col3 <- as.character(allcol[,3])
col4 <- as.character(allcol[,4])
col5 <- as.character(allcol[,5])
col6 <- as.character(allcol[,6])
col7 <- as.character(allcol[,7])
col8 <- as.character(allcol[,8])
col9 <- as.character(allcol[,9])
col10 <- as.character(allcol[,10])`
allcol <- c(col1,col2,col3,col4,col5,col6,col7,col8,col9,col10)
uniqueallcol <- unique(allcol)
allcol <- c(col1,col2,col3,col4,col5,col6,col7,col8,col9,col10)
uniqueallcol <- unique(allcol)
If all your columns have the same data type (numeric, for example), you can transform your data frame into a single vector with unlist
, remove duplicates in the resulting vector with unique
, and then turning it back into a data frame with data.frame
:如果所有列都具有相同的数据类型(例如数字),则可以使用
unlist
将数据框转换为单个向量,使用unique
删除结果向量中的重复项,然后将其转回包含数据的数据框data.frame
:
uniqueallcol <- data.frame("unique_col" = unique(unlist(allcol)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.