简体   繁体   中英

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. Now I am doing it in a stupid way, can anyone show me how to loop it? Sry I am so new to 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)

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 :

uniqueallcol <- data.frame("unique_col" = unique(unlist(allcol)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM