user11418708
user11418708

Reputation: 902

Subset data based on string values

I would like to subset my data frame based on the index column; I would like to keep those cases whose index is saved in myvar (eg. 110, 111). I don't understand why I receive 0 observations when running this code:

newdata <- df[ which(df$index=="myvars"), ]

Sample data:

df<-structure(list(index = c(111, 110, 101, 111), et = c(1, 1, 1, 
1), d1_t2 = c(0, 1, 1, 1), d1_t3 = c(0, 0, 1, 1), d1_t4 = c(0, 
1, 0, 1), d2_t1 = c(0, 0, 1, 1), d2_t2 = c(0, 1, 1, 1), d2_t3 = c(0, 
0, 0, 1), d2_t4 = c(1, 0, 1, 1), d3_t1 = c(1, 0, 1, 1), d3_t2 = c(1, 
1, 0, 1), d3_t3 = c(1, 0, 1, 1), d3_t4 = c(1, 1, 0, 1), d4_t1 = c(0, 
0, 1, 1), d4_t2 = c(1, 1, 0, 1), d4_t3 = c(0, 0, 1, 1), d4_t4 = c(1, 
0, 1, 1), d5_t1 = c(1, 0, 0, 1), d5_t2 = c(0, 1, 1, 1), d5_t3 = c(1, 
0, 1, 1), d5_t4 = c(0, 0, 1, 1), d6_t1 = c(1, 0, 0, 1), d6_t2 = c(0, 
0, 1, 1), d6_t3 = c(1, 0, 1, 1), d6_t4 = c(1, 0, 1, 1), d7_t1 = c(1, 
1, 1, 1), d7_t2 = c(1, 1, 1, 1), d7_t3 = c(1, 0, 1, 1), d7_t4 = c(1, 
0, 1, 1)), row.names = c(NA, 4L), class = "data.frame")

Code:

myvars<-c("110", "111")

Upvotes: 0

Views: 51

Answers (2)

one_observation
one_observation

Reputation: 464

There are several basic problems with what you are trying to do.

  1. You are not using the variable 'myvars' -- you are using a string with the value "myvars". None of your rows has the index "myvars".
  2. You are using == which is good for one value (e.g. values==4), but myvars has multiple values in it. Instead, you could use df$index %in% myvars
  3. This does work, but you have integer indices, and are accessing them with strings. This is unnecessary, and could lead to problems in other places.
  4. You may be confused because of your very large and complex example data. You only need one column to test -- not twenty.

Upvotes: 1

Wimpel
Wimpel

Reputation: 27732

try

myvars<-c(110, 111)  # <-- !! no quotes !!
df[ which(df$index %in% myvars ), ] #also, no quotes round myvars

Upvotes: 1

Related Questions