Re: [R] create new column to combine 2 data.frames

2013-08-30 Thread arun
IKAL Petr To: Mat ; "r-help@r-project.org" Cc: Sent: Friday, August 30, 2013 5:57 AM Subject: Re: [R] create new column to combine 2 data.frames Hi Sorry, I did not read your question to the end library(reshape) merge(dat1,cast(melt(dat2, c("ID", "Type")),

Re: [R] create new column to combine 2 data.frames

2013-08-30 Thread Mat
thanks. Works perfectly you made my day :-) -- View this message in context: http://r.789695.n4.nabble.com/create-new-column-to-combine-2-data-frames-tp4674963p4674994.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-projec

Re: [R] create new column to combine 2 data.frames

2013-08-30 Thread PIKAL Petr
ly change NA to 0 if you want. Regards Petr > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Mat > Sent: Friday, August 30, 2013 11:27 AM > To: r-help@r-project.org > Subject: Re: [R] create new column to c

Re: [R] create new column to combine 2 data.frames

2013-08-30 Thread PIKAL Petr
Hi see ?merge something like merge(df1, df2) Petr > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Mat > Sent: Friday, August 30, 2013 10:37 AM > To: r-help@r-project.org > Subject: [R] create new column to combine 2 data.

Re: [R] create new column to combine 2 data.frames

2013-08-30 Thread Rui Barradas
Hello, Ok, try instead library(reshape2) tmp <- dcast(data = dat2, ID ~ Type, value.var = "Type") tmp[-1] <- lapply(tmp[-1], function(x){ y <- integer(length(x)) s <- as.character(x[!is.na(x)])[1] idx <- which(as.character(dat2[["Type"]]) == s) y[!is.na(x)] <-

Re: [R] create new column to combine 2 data.frames

2013-08-30 Thread Mat
Thanks first. that doesn't look bad. But if have a equal ID in dat2, the days are no longer correct. the correct data.frame has to look like this one: ID Name Management Training 1 1 Jack 13 2 2 John 10 3 3 Jill 04 not this one: ID Name

Re: [R] create new column to combine 2 data.frames

2013-08-30 Thread Rui Barradas
Hello, Suposing that your data frames are named dat1 and dat2, the following works, but it's a bit complicated, maybe there are simpler solutions. dat1 <- read.table(text = " ID Name 1 Jack 2 John 3 Jill ", header = TRUE, stringsAsFactors = FALSE) dat2 <- read.table(text = " ID Days Ty