Hello!

# I have a data frame:
mydf<-data.frame(c1=rep(NA,5),c2=rep(NA,5),c3=rep(NA,5))

# I have an index whose length is always the same as nrow(mydf):
myindex<-c(1,2,3,2,1)

# I need c1 to have 1s in rows 1 and 5 (based on the information in myindex)
# I need c2 to have 1s in rows 2 and 4 (also based on myindex)
# I need c3 to have 1 in row 3
# In other words, I am trying to achieve this result:
mygoal<-data.frame(c1=c(1,NA,NA,NA,1),c2=c(NA,1,NA,1,NA),c3=c(NA,NA,1,NA,NA))

I know how to do it with a loop that runs through rows of mydf.
However, in real life I have a huge data frame with tons of rows, dozens of
columns (instead of 3 in this example) - I am afraid it'll take forever.
Any hint on how to do it faster, maybe using subindexing somehow?

Thank you very much!

-- 
Dimitri Liakhovitski

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to