简体   繁体   English

如何在R中选择案例?

[英]How to select cases in R?

I'm keen to make the leap from SPSS to R.我渴望实现从 SPSS 到 R 的飞跃。

A common command used in SPSS is applying filtering. SPSS 中使用的一个常用命令是应用过滤。 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:我试图在 SPSS 语法中复制的过滤器是:

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 https://www.statmethods.net/management/subset.html

Grateful for any suggestions.感谢任何建议。

Thanks.谢谢。

Instead of 2019dataset$income~=0而不是2019dataset$income~=0

try 2019dataset$income!=0 if you want "not equal to"如果您想要“不等于”,请尝试2019dataset$income!=0

or 2019dataset$income==0 if you want "equal to"或者2019dataset$income==0如果你想要“等于”

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空格可能会使阅读更清晰,因此2019dataset$income != 02019dataset$income == 0将是一个改进,您可能不需要which ,但这些不太重要

I transitioned from SPSS to R and I prefer to use the tidyverse package, which I think is a bit more intuitive.我从 SPSS 过渡到 R,我更喜欢使用 tidyverse 包,我认为它更直观一些。

Your code would look something like:您的代码如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM