-2

Currently I am working with a dataset in r that I imported from a SPSS file converted to a csv. The data includes multiple factors such as gender, ethnicity, and test group, along with a set of weights I want to sum. I want to sum these weights based on multiple conditions (i.e. female + white + group1) so I tried subsetting the data.

small.set<-subset(df, df[,"gender"]==1 & df[,"ethnicity"] ==1 & 
df[,"group"==1])

However, I get the following error:

Error in matrix(unlist(value, recursive = FALSE, use.names = FALSE), nrow = nr,
: 'data' must be of a vector type, was 'NULL'

I found that when trying to select group 1 in any case, R returned strange results:

 df["group"==1]
 > data frame with 0 columns and 619 rows

The structure of "group" is as follows:

str(df["group")
>Factor w/ 3 levels "1", "2", "3": 1 3 1 1 2...

Does anyone know what is causing this to happen?

Syrah.Sharpe
  • 103
  • 1
  • 8
  • 3
    It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Are you sure you have the column names of your data.frame correct (R is case sensitive)? – MrFlick Aug 25 '17 at 18:34
  • `"group"==1` is always `FALSE`. Maybe you want `small.set["group"]==1`. – Rui Barradas Aug 25 '17 at 18:36
  • In your first instruction you're using two df's with `subset`. Don't do that. Do it one at a time. Also, with `subset` you don't need to repeat the name of the df, `gender == 1 & group == 1` will do. – Rui Barradas Aug 25 '17 at 18:38
  • @RuiBarradas Sorry, that was a typo as I changed the names of the dataset to more general terms. Let me fix that now. – Syrah.Sharpe Aug 25 '17 at 18:47
  • @Syrah.Sharpe could your print out the head of your data? That would make it a lot easier to figure out what's going on. – Kristofersen Aug 25 '17 at 18:49
  • @RuiBarradas "group"==1 was the problem that I was having. Changing that fixed everything! Thanks!! – Syrah.Sharpe Aug 25 '17 at 18:53

1 Answers1

0

why dont you aboid using sample and use directly:

small.set<-df[df$gender == 1 & df$ethnicity == 1 && df$group == 1,]

Another good way is using data.table package:

library(data.table)

df<-data.table(df)
small.set<-df[.(gender==1,ethnicity == 1,group == 1)]
Jesus
  • 462
  • 6
  • 13