0

I have 6 data frames with 2 columns each for "id" and "value" and differing number of rows. They are labeled p1, p2....p6 and look like this

id          value_p1
Jane C      9.713457e-01
Claire K    1.260160e-01
Brett F     4.933005e-0
Jen S       0.56

I now have a character vector that includes the intersect of names across all 6 data sets called id_ intersect:

c("Jane C", "Claire K","Brett F") 

I now want to create a new data frame that has is a subset of only the sections in each data frame that contain id_intersect and keeps the values for the intersecting ids in each data frame.

id          value_p1      value_p2.............value_p6
Jane C      9.713457e-01  0.87                 .098
Claire K    1.260160e-01  0.89                 .005
Brett F     4.933005e-0   0.002                .035

I'm really stuck on this and I'm pretty new to R so any help is much appreciated. Thanks.

CadisEtRama
  • 1,071
  • 5
  • 18
  • 31
  • Your question is not [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so helping you will be difficult. It sounds like you may want to get all of your data.frames into one object via `rbind` or `merge` or some other related function...then you can simply subset the data once. – Chase Apr 05 '13 at 18:04

1 Answers1

2

You can use Reduce here with merge,

 ll <- list(p1,p2,...,p6)
 Reduce(merge,ll) ## 

Here a re producible example. First I create your data frames within a list

ll <- lapply(1:6, function(x) {
  id <- c(letters[1:3],letters[3+x])
  dat <- data.frame(id=id, value=rnorm(4))
  names(dat) <- c('id',paste('value',x,sep='_'))
  dat

})

Then

Reduce(merge,ll)
        id     value_1     value_2    value_3   value_4      value_5    value_6
1  Brett F -0.09242725 -0.03908275  0.5366957 0.4926749  0.830829230  1.4868564
2 Claire K -0.27487913 -0.13733120 -0.2708968 0.1720550 -0.003194644 -0.6328486
3   Jane C -1.56306487 -0.75655434  0.1806619 0.2482159 -1.075606294  1.9711559
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • o wow, THANK you, I just spent 2 hours looking for a code to do this and you solved it! thank you so much!! – CadisEtRama Apr 05 '13 at 18:54
  • You are welcome. Please try to put a reproducible example next time. Read the link in @Chase commment – agstudy Apr 05 '13 at 18:56