[R] combine 2 data.frames in dependence of the ID

arun smartpink111 at yahoo.com
Wed Feb 26 16:42:11 CET 2014


Hi Mat,


Please check the str() of your dat2.

If I change 

dat2$DATE <- as.POSIXlt(dat2$DATE)
 dcast(ddply(merge(dat1,dat2,by="FS_ID")[,-1],.(ABNR), mutate, DATE=seq_along(DATE)),ABNR~DATE,value.var="POST")
#Error in attributes(out) <- attributes(col) : 
#  'names' attribute [9] must be the same length as the vector [3]

A.K.



On Wednesday, February 26, 2014 9:57 AM, Matthias Weber <Matthias.Weber at fntsoftware.com> wrote:
Thanks first for the help,

Your description works with the example perfect. If I try it with my data, there is an error.

error in attributes(out) <- attributes(col) : 
  attribute 'names' [306] must have the same length, than the vector [1]

My data.frame "dat2" has 306 lines, could be this the reason? What does R mean with the vector?

Thanks a lot for the help.

Best regards. Mat.


-----Ursprüngliche Nachricht-----
Von: arun [mailto:smartpink111 at yahoo.com] 
Gesendet: Mittwoch, 26. Februar 2014 10:08
An: Matthias Weber
Betreff: Re: [R] combine 2 data.frames in dependence of the ID

#or you could do: 

dcast(ddply(merge(dat1,dat2,by="FS_ID")[,-1],.(ABNR), mutate, DATE=cut(seq_along(DATE),breaks=c(0,1,2,3,4,5,6,7,8,9),labels=c("FIRST","SECOND","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"))),ABNR~DATE,value.var="POST")
#      ABNR FIRST SECOND THREE
#1 33688812    28     29  28.3
#2 33800000    10     NA    NA

A.K.




On Wednesday, February 26, 2014 3:58 AM, arun <smartpink111 at yahoo.com> wrote:
Try:
library(plyr)
library(reshape2)
res <- dcast(ddply(merge(dat1,dat2,by="FS_ID")[,-1],.(ABNR), mutate, DATE=seq_along(DATE)),ABNR~DATE,value.var="POST")

colnames(res)[-1] <- c("FIRST","SECOND","THREE")
 res
#      ABNR FIRST SECOND THREE
#1 33688812    28     29  28.3
#2 33800000    10     NA    NA

A.K.




On Wednesday, February 26, 2014 3:06 AM, Mat <matthias.weber at fnt.de> wrote:
thanks for the help up to here.

A little problem remains.

I have different "ABNR", if i try it with another ABNR, the Column extend for each ABNR, it should start with "FIRST" again.

dat1 <- read.table(text="FS_ID  ABNR
9327    33688812
11391  33688812
11392  33688812
11388  33688812
11390  33688812
12028  33688812
12029  33688812
19999  33800000",sep="",header=TRUE)

dat2 <- read.table(text="FS_ID  DATE              POST
11390  2012-12-13    28
12029  2013-01-17    28.3
11391  2011-02-20    29
19999  2014-02-20    10",header=TRUE,stringsAsFactors=FALSE)
library(reshape2)
setNames(dcast(merge(dat1,dat2,
by="FS_ID")[,-1],ABNR~DATE,value.var="POST"),c("ABNR","FIRST","SECOND")) 

      ABNR FIRST SECOND THREE NA
1 33688812    29     28  28.3 NA
2 33800000    NA     NA    NA 10

it shoult start for each ABNR in the "FIRST"-Column again.

Right would be:

      ABNR FIRST SECOND THREE
1 33688812    29     28  28.3
2 33800000    10     NA    NA 

Thank you.



--
View this message in context: http://r.789695.n4.nabble.com/combine-2-data-frames-in-dependence-of-the-ID-tp4685781p4685855.html

Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list