0

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")
1
  • 1
    In your example you search for the string "myvars", which is not in your data.frame. You have to use your vector myvars. And btw, if your df entries are numeric, myvars entries also have to be numeric. Commented Feb 25, 2021 at 10:53

2 Answers 2

1

try

myvars<-c(110, 111)  # <-- !! no quotes !!
df[ which(df$index %in% myvars ), ] #also, no quotes round myvars
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.