[R] Logical vector-based extraction

Muhuri, Pradip (SAMHSA/CBHSQ) Pradip.Muhuri at samhsa.hhs.gov
Sat Nov 3 22:41:11 CET 2012


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")]



More information about the R-help mailing list