[R] problem subsetting data.frame in R version 2.15.2 for Windows

Rui Barradas ruipbarradas at sapo.pt
Thu Mar 21 00:39:09 CET 2013


Hello,

Inline.

Em 20-03-2013 22:17, Borja . escreveu:
> Good day.
> I create a data frame like this:
>                > data <- data.frame(a=1:10,b=11:20,c=21:30)
> I can subset this data.frame by saying:
>                > data[data$a>7,]
> and I get this result
>                            a  b  c                    8   8 18 28                    9   9 19 29                  10 10 20 30
> I understand I should get the same result by saying
>                 > data[a>7,0]
> but I don't.

Because what you understand is wrong. There is no object 'a'. The error 
message is very clear. What you have is an element of the data.frame 
named 'a', that can be accessed like data$a or data[["a"]].
Use the first way of subsetting the data.frame.

Also, 'data' is a bad name for an object, it already is a function name.


Hope this helps,

Rui Barradas

  Instead I get:
>                  Error in `[.data.frame`(data, a > 7, 0) : object 'a' not found
> Thank you very much in advance 		 	   		
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list