0

'data' is a large dataset. I want to use subset-function to only get some user's in 'data'.

dim(subset(data, data$user == c(1,2)))

I get 110 12.

dim(subset(data, data$user==1))

I get 9 12 and for

dim(subset(data, data$user==2))

I get 214 12.

This is strange because 9+214 is not equal 110 so it seems like I can't use the subset-function to get more than one user. How can that be?

Ole Petersen
  • 670
  • 9
  • 21
  • 1
    You can use `%in%` for multiple elements ie. `subset(data, user %in% 1:2)` Another way would be `subset(data, user==1 | user==2)` Also, inside the `subset`, you don't have to use `data$`. – akrun Aug 10 '15 at 13:01
  • For these two it works but when I do it for more it say "longer object length is not a multiple of shorter object length" – Ole Petersen Aug 10 '15 at 13:19
  • Usually, it should work with `%in%`. But, I can't comment without a reproducible example. – akrun Aug 10 '15 at 13:31
  • 1
    If you have more than one value to compare against just use `%in%` and get over with. I can't think of any case it won't work. The warning message you are getting is for using `==` in inappropriate manner. – David Arenburg Aug 10 '15 at 13:31
  • Thanks for the response. It works fine but how can I do it if I want entries in a vector like u[1], u[2],.., u[10] for example. What should I do then ? I can't use %in% u[1]:u[10] because it would simply take all from u[1] to u[10] but I only want u[1], u[2],..., u[10]. – Ole Petersen Aug 10 '15 at 13:49

0 Answers0