[R] Data frame index?

Petr Pikal petr.pikal at precheza.cz
Wed Jan 18 09:50:13 CET 2006


Hi

eg. your data frame has 35 rows and 6 columns

a<-sample(1:6, 35, replace=T)
b<-1:35
vec<-rep(0,35*6)
vec[a+6*(b-1)]<-1

This shall do the replacement
your.d.f[matrix(vec,35,6, byrow=T)==1] <- 0

But I am not sure if it is quicker than a loop.

HTH
Petr


On 18 Jan 2006 at 2:35, Kenneth Cabrera wrote:

Date sent:      	Wed, 18 Jan 2006 02:35:35 -0500
From:           	Kenneth Cabrera <krcabrer at epm.net.co>
To:             	r-help at stat.math.ethz.ch
Subject:        	[R] Data frame index?

> 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
> 
> 

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list