Associating columns with identical column names in the same data frame in R

I have a data frame that looks something like this:

df <- data.frame(0:2, 1:3, 2:4, 5:7, 6:8, 2:4, 0:2, 1:3, 2:4) colnames(df) <- rep(c('a', 'b', 'c'), 3) > df abcabcabc 1 0 1 2 5 6 2 0 1 2 2 1 2 3 6 7 3 1 2 3 3 2 3 4 7 8 4 2 3 4 

There are several columns with the same name. I would like to reorder the data frames so that columns with the same names are combined into their own supercolumn, so that only unique column names remain, for example:

 > df abc 1 0 1 2 2 1 2 3 3 2 3 4 4 5 6 2 5 6 7 3 6 7 8 4 7 0 1 2 8 1 2 3 9 2 3 4 

Any thoughts on how to do this? Thanks in advance!

+6
source share
5 answers

It will be a trick, I suppose.

Explanation

df[,names(df) == 'a'] will select all columns named a

unlist converts the above columns into one single vector

unname will remove some street unname names given to these vectors.

unique(names(df)) will give you unique column names in df

sapply apply the built-in function to all unique(names(df)) values

 > df abcabcabc 1 0 1 2 5 6 2 0 1 2 2 1 2 3 6 7 3 1 2 3 3 2 3 4 7 8 4 2 3 4 > sapply(unique(names(df)), function(x) unname(unlist(df[,names(df)==x]))) abc [1,] 0 1 2 [2,] 1 2 3 [3,] 2 3 4 [4,] 5 6 2 [5,] 6 7 3 [6,] 7 8 4 [7,] 0 1 2 [8,] 1 2 3 [9,] 2 3 4 
+7
source

My version:

 library(reshape) as.data.frame(with(melt(df), split(value, variable))) abc 1 0 1 2 2 1 2 3 3 2 3 4 4 0 1 2 5 1 2 3 6 2 3 4 7 0 1 2 8 1 2 3 9 2 3 4 

In step using melt I will transform the data set:

 > melt(df) Using as id variables variable value 1 a 0 2 a 1 3 a 2 4 b 1 5 b 2 6 b 3 7 c 2 8 c 3 9 c 4 10 a 0 11 a 1 12 a 2 13 b 1 14 b 2 15 b 3 16 c 2 17 c 3 18 c 4 19 a 0 20 a 1 21 a 2 22 b 1 23 b 2 24 b 3 25 c 2 26 c 3 27 c 4 

Then I split the value column for each unique variable level using split :

 $a [1] 0 1 2 0 1 2 0 1 2 $b [1] 1 2 3 1 2 3 1 2 3 $c [1] 2 3 4 2 3 4 2 3 4 

for this you will need as.data.frame to become the necessary data structure.

+5
source

Use %in% and some not listed

 zz <- lapply(unique(names(df)), function(x,y) as.vector(unlist(df[which(y %in% x)])),y=names(df)) names(zz) <- unique(names(df)) as.data.frame(zz) abc 1 0 1 2 2 1 2 3 3 2 3 4 4 5 6 2 5 6 7 3 6 7 8 4 7 0 1 2 8 1 2 3 9 2 3 4 
+2
source

I would sort data.frame by name, list, and column using as.data.frame on matrix :

 A <- unique(names(df))[order(unique(names(df)))] B <- matrix(unlist(df[, order(names(df))], use.names=FALSE), ncol = length(A)) B <- setNames(as.data.frame(B), A) B # abc # 1 0 1 2 # 2 1 2 3 # 3 2 3 4 # 4 5 6 2 # 5 6 7 3 # 6 7 8 4 # 7 0 1 2 # 8 1 2 3 # 9 2 3 4 
+2
source

Now I am not on the computer, so I can’t check it, but ... it might work:

 do.call(cbind, lapply(names(df) function(x) do.call(rbind, df[, names(df) == x])) ) 
0
source

All Articles