[英]Combine contents in different columns into one column
親愛的堆棧溢出天才,
我正在嘗試將包含 10 列的 data.frame 合並為 1 列,然后刪除重復項。 現在我正在以一種愚蠢的方式進行操作,有人可以告訴我如何循環嗎? 對不起,我對 R 很陌生。 太感謝了!
數據很簡單,這里只是一個例子
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)
如果所有列都具有相同的數據類型(例如數字),則可以使用unlist
將數據框轉換為單個向量,使用unique
刪除結果向量中的重復項,然后將其轉回包含數據的數據框data.frame
:
uniqueallcol <- data.frame("unique_col" = unique(unlist(allcol)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.