[R] Consolidate column contents of equally "named" columns

Daniel Malter daniel at umd.edu
Sat Apr 28 16:46:56 CEST 2012


Hi,

I have a data frame whose first row (not the header) contains the true
column names. The same column name can  occur multiple times in the dataset.
Columns with equal names are not adjacent, and for each observation only one
of the equally named columns contains the actual data (see the example
below). I am looking for an easy method consolidate these columns into one
column for each unique column name. Say,

x1<-c("x",1,NA,NA)
x2<-c("x",NA,2,NA)
x3<-c("x",NA,NA,3)
y1<-c("y",3,NA,NA)
y2<-c("y",NA,1,NA)
y3<-c("y",NA,NA,2)
d<-data.frame(x1,y1,x2,y2,x3,y3)
d

# d looks like:

    x1   y1   x2   y2   x3   y3
1    x    y    x    y    x    y
2    1    3 <NA> <NA> <NA> <NA>
3 <NA> <NA>    2    1 <NA> <NA>
4 <NA> <NA> <NA> <NA>    3    2

>From this, I want to create the table or data frame

x y
1 3
2 1
3 2

I would appreciate your help.

Daniel


--
View this message in context: http://r.789695.n4.nabble.com/Consolidate-column-contents-of-equally-named-columns-tp4594852p4594852.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list