Johnny
Johnny

Reputation: 91

How to select cases in R?

I'm keen to make the leap from SPSS to R.

A common command used in SPSS is applying filtering. Can somebody please advise on why I am receiving an error message?

2019dataset=read.spss("C:\\SPSS data\\2019dataset.sav")

selected_2019dataset <- 2019dataset[ which(2019dataset$hhweight > 0 & 2019dataset$income~=0 & 2019dataset$age > 16 & 2019dataset$age < 59),]

I'm getting an error saying that there is an unexpected '='

The filter I am trying to replicate in SPSS syntax is:

SELECT IF ((hhweight > 0) AND (income~=0) AND (age > 16 AND age <59)).

I've been following the example here:

https://www.statmethods.net/management/subset.html

Grateful for any suggestions.

Thanks.

Upvotes: 2

Views: 2552

Answers (2)

user11210091
user11210091

Reputation:

I transitioned from SPSS to R and I prefer to use the tidyverse package, which I think is a bit more intuitive.

Your code would look something like:

library(tidyverse)    
selected_2019dataset <- 2019dataset %>%
    filter(hhweight > 0 & income == 0 & age > 16 & age < 59)

Upvotes: 2

Henry
Henry

Reputation: 6784

Instead of 2019dataset$income~=0

try 2019dataset$income!=0 if you want "not equal to"

or 2019dataset$income==0 if you want "equal to"

Spaces might make reading clearer so 2019dataset$income != 0 or 2019dataset$income == 0 would be an improvement, and you may not need which, but these are less important

Upvotes: 3

Related Questions