[R] indexing question

baptiste auguie ba208 at exeter.ac.uk
Tue Jan 13 10:38:09 CET 2009


you can also look at subset,


> 	my.data.frame <- data.frame(a=rnorm(10),  
> b=factor(sample(letters[1:4], 10, replace=T)))
> 	str(my.data.frame)
> 	my.data.frame[my.data.frame$b == "a", ]
> 	subset(my.data.frame, b == "a")

by the way, it is probably safer not to use "data" as a variable name  
as it is also a function.


Hope this helps,

baptiste

On 13 Jan 2009, at 09:33, Dimitris Rizopoulos wrote:

> have a look at ?"[.data.frame"; what you need is the following:
>
> dat <- data.frame(a = rbinom(20, 1, 0.5), x = rnorm(20), y =  
> rnorm(20))
>
> dat
> dat[dat$a == 1, ]
>
>
> I hope it helps.
>
> Best,
> Dimitris
>
>
> Bunny, lautloscrew.com wrote:
>> Hi all,
>>
>> i have a pretty easy indexing question, at least i believe so. The  
>> main
>> reason i post it here, is that brackets and $ are hard to google.
>>
>> How do I index correctly, if i just want to display the whole dataset
>> conditioned on the fact that some particular column equals one.
>>
>> I know i can do something like:   data$somecolumn[data$particularcol
>> ==1] . That will show all "some column" values where the particular
>> column is 1.
>> Unfortunately something like : data[data$particularcol ==1] does not
>> work to get the whole matrix.
>>
>> is there some easy way except the % in % stuff ?
>>
>> Thx in advance
>>
>> ______________________________________________
>> R-help at 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.
>>
>
> --
> Dimitris Rizopoulos
> Assistant Professor
> Department of Biostatistics
> Erasmus Medical Center
>
> Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
> Tel: +31/(0)10/7043478
> Fax: +31/(0)10/7043014
>
> ______________________________________________
> R-help at 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.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag




More information about the R-help mailing list