0

I would like to paste values of a certain data.frame row to other rows which have a certain attribute of a certain feature, however not a whole row just a couple of values of it. Exactly it looks like:

z <- c(NA, NA, 3,4,2,3,5)
x <- c(NA, NA, 2,5,5,3,3)
a <- c("Hank", NA, NA, NA, NA, NA, NA)
b <- c("Hank", NA, NA, NA, NA, NA, NA)
c <- c(NA, NA, NA, NA, NA, NA, NA)
d <- c("Bobby", NA, NA, NA, NA, NA, NA)
df <- as.data.frame(rbind( a, b, c, d, z, x))

Now, I would like to pass df["z",3:7] to the rows[3:7] which have V1 == "Hank", and pass df["x", 3:7] when V1== "Bobby".

Do anybody has a hint for me? I guess it should be a function with sapply or something like that. Maybe a dplyr could give a solution? Thanks for any advice!

KriKox
  • 43
  • 8
  • 1
    not sure where you are getting raw data and why you need this approach. It looks like `merge` may be good approach. but to achieve what you show here `df[df$V1 %in% c("Hank"),3:7]<-z[3:7]` and `df[df$V1 %in% c("Bobby"),3:7]<-x[3:7]` should give you the result – Ananta Feb 24 '16 at 15:57
  • oh yes, your advice did it, thanks! – KriKox Feb 24 '16 at 16:20
  • However, I do not understand, why my try didn't do it, like df[df$V1=="Hank",3:7] <- z[3:7] – KriKox Feb 24 '16 at 16:22
  • df[df$V1=="Hank",3:7] <- z[3:7] – KriKox Feb 24 '16 at 19:04
  • that still keeps any NA's in V1 and I guess that is the problem – Ananta Feb 24 '16 at 19:12

0 Answers0