[R] Data frame referencing?

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Fri Aug 4 14:11:08 CEST 2006


you need to use quotes, i.e.,

a[a$y > 0.5, "y"] <- 1

you can also use

a$y[a$y > 0.5] <- 1


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Sander Oom" <slist at oomvanlieshout.net>
To: <r-help at stat.math.ethz.ch>
Sent: Friday, August 04, 2006 1:48 PM
Subject: [R] Data frame referencing?


> Dear R users,
>
> When you do:
>> x <- rnorm(10)
>> y <- rnorm(10)
>> z <- rnorm(10)
>> a <- data.frame(x,y,z)
>> a$x
> [1]  1.37821893  0.21152756 -0.55453182 -2.10426048 -0.08967880 
> 0.03712110
> [7] -0.80592149  0.07413450  0.15557671  1.22165341
>
> Why does this not work:
>> a[a$y>0.5,y] <-1
> Error in "[<-.data.frame"(`*tmp*`, a$y > 0.5, y, value = 1) :
>        only 0's may be mixed with negative subscripts
>
> While this works:
>> a[a$y>0.5,2] <-1
>
>> a
>             x          y          z
> 1   1.37821893 -1.0887363  1.7340522
> 2   0.21152756 -0.7256467 -1.3165373
> 3  -0.55453182  1.0000000 -2.1116072
> 4  -2.10426048 -0.4898596 -1.5863823
> 5  -0.08967880  1.0000000 -0.9139706
> 6   0.03712110  1.0000000 -1.3004970
> 7  -0.80592149 -0.7004193 -0.1958059
> 8   0.07413450  1.0000000 -1.3574303
> 9   0.15557671 -0.3335407 -2.1991236
> 10  1.22165341  1.0000000 -0.7576708
>
> For a complex loop I would prefer to reference the right colomn by 
> name,
> not by number! Now, when the colomns change, I need to check my code 
> to
> make sure that the right colomns are referenced.
>
> Suggestions much appreciated!
>
> Thanks in advance,
>
> Sander.
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list