首页 > 解决方案 > R将相同的因子值视为不同的值

问题描述

我是 R 的新用户,正在尝试对我的一个专栏进行子集化。但是,某些值丢失/未子集到新子集。

我尝试了代码拼写的不同变体,但它似乎不起作用(即:)

df_Location = df[df$Location == "Samarinda" | df$Location == "Samarinda " df$Location == "Samarinda. " df$Location == " Samarinda",]
df_Location
summary(df)

df_Location = df[df$Location == "Samarinda",]
df_Location
summary(df)

df_Location = df[df$Location == "Samarinda",]
df_Location
summary(df)

这些代码只产生了 7 行的子集 --> 原始数据中应该有 37 行

当我使用 rPivotTable 时,这就是它所显示的内容(Samarinda 列出了两次,值分别为 30 和 7):

Samarinda   30
Samarinda   7
Totals  221

谁能建议如何解决这个问题?

非常感谢

标签: r

解决方案


grepping 的替代方法可以通过 运行字符串trimws,如下所示:

df_Location = df[trimws(df$Location) == "Samarinda",]

推荐阅读