Reputation: 11
Dear Stack overflow genius,
I am trying to combine a data.frame
containing 10 columns into 1 column and then remove duplicates. 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)
Upvotes: 0
Views: 28
Reputation: 1430
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)))
Upvotes: 1