通过 R 中的因子变量对数据框进行子集化时遇到问题

奥克斯泰夫
  • 我试图通过因子变量 $area__rucc 将我的数据框(火车)子集分为两组:地铁和非地铁。这个数据框是干净的,有 34 个变量和 2,811 个观察值。

     glimpse(train$area__rucc)
    

    因子 w/ 9 个级别“都会区 - 100 万或以上人口的都会区县”,..:3 3 1 6 7 8 6 2 7 5 ...

前三层表示地铁,后六层表示非地铁

- 首先,我尝试通过地铁进行子集...

metro <- subset(train, area__rucc == c("Metro - Counties in metro areas of 1 million population or more", "Metro - Counties in metro areas of 250,000 to 1 million population", "Metro - Counties in metro areas of fewer than 250,000 population"))

这似乎按预期工作并返回了 df 与 387 个观察值。

- 接下来,我尝试按这样的非地铁级别进行子集...

not_metro <- subset(train, area__rucc != c("Metro - Counties in metro areas of 1 million population or more", "Metro - Counties in metro areas of 250,000 to 1 million population", "Metro - Counties in metro areas of fewer than 250,000 population"))

这返回了 2811 个观测值,但经过进一步检查,df 包含地铁级别和非地铁级别。显然没有按我的意图工作。

- 我的第三枪...

non_metro <- subset(train, area__rucc == c("Nonmetro - Completely rural or less than 2,500 urban population, adjacent to a metro area", 
                "Nonmetro - Completely rural or less than 2,500 urban population, not adjacent to a metro area", 
                "Nonmetro - Urban population of 2,500 to 19,999, adjacent to a metro area", 
                "Nonmetro - Urban population of 2,500 to 19,999, not adjacent to a metro area", 
                "Nonmetro - Urban population of 20,000 or more, adjacent to a metro area", 
                "Nonmetro - Urban population of 20,000 or more, not adjacent to a metro area"))

在这里,我明确列出了非地铁级别 (4:9)。这返回了一个包含 354 个观测值的 df,所有观测值都是非地铁的。

387(地铁)+ 354(非地铁)!= 3189 train$area_rucc 中没有缺失值,所以我试图从 train 创建的两个 df 应该与原始 df 保持相同数量的观察,对吗?

我有一种感觉,我犯了一个愚蠢的错误,我现在似乎无法抓住(可能缺乏经验),或者我可能完全偏离了我在这里尝试做的事情,但这开始令人沮丧我,任何见解将不胜感激。

格雷戈尔·托马斯

==做逐元素(按行)比较-你想%in%,而不是

在我们进入你的代码之前,让我们做一个简单的例子

x = 1:6
y = c(1, 3)
x == y
# [1]  TRUE FALSE FALSE FALSE FALSE FALSE

请注意如何只有一个TRUE,即使 1 和 3 都在1:6. 那是因为比较是这样发生的:

data.frame(x, y, "x==y" = x == y, check.names = FALSE)
#   x y  x==y
# 1 1 1  TRUE   # 1 does equal 1
# 2 2 3 FALSE   # 2 does not equal 3
# 3 3 1 FALSE   # 3 does not equal 1
# 4 4 3 FALSE   # 4 does not equal 3
# 5 5 1 FALSE   # 5 does not equal 1
# 6 6 3 FALSE   # 6 does not equal 3

x == y检查的所述第一元件x相对于第一的y,第二x对的第二y,等等。如果一个xy较短,它将被“回收”一样可以在上述在输入数据帧看y = c(1, 3)成为1 3 1 3 1 3在数据框架。

相反,使用%in%

x %in% y
# [1]  TRUE FALSE  TRUE FALSE FALSE FALSE

x %in% yx对照 的所有元素检查 的每个元素y现在我们得到两个 TRUE 值,因为 1 和 3 都 c(1, 3)


应用于您的问题:

metro <- subset(train,
    area__rucc %in% c(
        "Metro - Counties in metro areas of 1 million population or more",
        "Metro - Counties in metro areas of 250,000 to 1 million population",
        "Metro - Counties in metro areas of fewer than 250,000 population"
    )
)

你可以否定它,! x %in% y,所以

not_metro <- subset(train,
        !area__rucc %in% c(
            "Metro - Counties in metro areas of 1 million population or more",
            "Metro - Counties in metro areas of 250,000 to 1 million population",
            "Metro - Counties in metro areas of fewer than 250,000 population"
        )
    )

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过R中多个变量中的多个值对数据进行子集

如何通过R中的三个变量对数据框进行排序和计数?

通过匹配列和值 R 对数据框进行子集和分组

想要通过在循环中对数据框进行子集化并根据 i 值分配每个数据框名称来在 R 中创建新数据框

通过R中的列内的唯一值对数据帧进行子集

无法通过在 R 中使用带有选择器的 cbind() 对数据框进行子集化

通过因子相关数之和对R中的数据进行排序

通过R中的数据对因子水平进行重新排序

如何根据R中的多个变量对数据框进行子集化

通过数据的间隔,用 R 对数据帧进行子集化

如何通过在R中有效过滤和分组来对数据进行子集

通过数据框列条件在列表中对数据框进行子集

通过部分匹配r中的另一个数据帧来对数据帧进行子集设置(对python / pandas解决方案开放)

通过将数据帧匹配到列表来对数据帧进行子集设置,并使用R在输出中也包含不匹配值

如何通过底数R中的数据框中的数字变量的中位数对框图中的框进行排序

通过预选的数据子集在R中进行循环

通过 R 中的分组因子将数值添加到数据框

R:根据日期列在另一列的因子级别内对数据框进行子集化

如何通过R中的可变列数进行子集/求和

R:通过匹配A列中的值进行子集

通过忽略r中的特定字符,对数据框的列执行操作

通过匹配R中的嵌套列表来子集和加入数据框

通过过滤不等于R中的值的子集来创建多个数据框

通过 R 中匹配的因子水平传播

通过R中的因子向量求和

如何在不影响R中数据框现有维度的情况下通过操作(均值)按日期对数据进行分组?

通过R中的名称为数据帧列表创建子集

根据 r 中的因子水平索引对数据框进行排序

如何仅通过r中的[]选择子集?