[R] Logical vector-based extraction

jim holtman jholtman at gmail.com
Sat Nov 3 22:54:07 CET 2012


works fine for me creating:

> state_pflt200 <- df$p_fatal <200
>  df[state_pflt200, c("state.name","p_fatal")]
[1] state.name p_fatal
<0 rows> (or 0-length row.names)
>

considering that there were no values less than 200 in your data, the
result is correct.

So what is the problem?

On Sat, Nov 3, 2012 at 5:41 PM, Muhuri, Pradip (SAMHSA/CBHSQ)
<Pradip.Muhuri at samhsa.hhs.gov> wrote:
>
> Hello,
>
> The most part of the program works except that the following logical variable does not get created although the second logical variable-based extraction works.
>
>  I don't understand what I am doing wrong here.
>
> state_pflt200 <- df$p_fatal <200
> df[state_pflt200, c("state.name","p_fatal")]
>
>
> I would appreciate receiving your help.
>
> Thanks,
>
> Pradip Muhuri
>
>
>
>
> ##### Below is the code that includes the reproducible example.
>
> df <- data.frame (state.name=
>                   c("Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut",
>                   "Delaware","DC", "Florida","Georgia","Hawaii","Idaho","Illinois","Indiana",
>                   "Iowa","Kansas","Kentucky",   "Louisiana","Maine","Maryland","Massachusetts","Michigan",
>                   "Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire",
>                   "New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma",
>                   "Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas",
>                    "Utah", "Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"),
>
>                    p_fatal = sample(200:500,51,replace=TRUE),
>
>                    t_safety_score = sample(1:10,51,replace=TRUE)
>                   )
>
> options (width=120)
>
>
>
> # The following logical variable does not get created - Don't understand what I am doing wrong
> state_pflt200 <- df$p_fatal <200
> df[state_pflt200, c("state.name","p_fatal")]
>
> # The following works
> state_sslt5 <- df$t_safety_score <5
> df[state_sslt5,c("state.name", "t_safety_score")]
>
> ______________________________________________
> 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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.




More information about the R-help mailing list