[R] Data frame index?

Jacques VESLOT jacques.veslot at cirad.fr
Wed Jan 18 09:09:47 CET 2006


try:

DF2 <- as.data.frame(matrix(vec, nr=nrow(DF),nc=ncol(DF))==
            matrix(1:ncol(DF),nr=nrow(DF),nc=ncol(DF),byrow=T))

DF3 <- data.frame(mapply(function(z,x,y) { x[y] <- 0 ; x },
   names(DF), DF, DF2, SIMPLIFY=F))

but there must be an easier way...


Kenneth Cabrera a écrit :

> Hi, R users:
>
> I have a data.frame (not a matrix), I got a vector with the same 
> length as the
> number of records (rows) of the data frame, and each element of
> that vector is the column number (in a specific range of columns) of 
> the corresponding
> record that I must set to zero.
>
> How can I  do this without a "for" loop?
>
> Thank you for your help.
>
> Kenneth
>
>------------------------------------------------------------------------
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list