r - 根据单列中的唯一值和非唯一值创建表
问题描述
给定具有以下结构的 CSV,
id, postCode, someThing, someOtherThing
1,E3 4AX, cats, dogs
2,E3 4AX, elephants, sheep
3,E8 KAK, mice, rats
4,VH3 2K2, humans, whales
我希望根据postCode
列中的值是否唯一创建两个表。其他列的值对我来说无关紧要,但必须将它们复制到新表中。
我的最终数据应该是这样的,有一个基于 unique postCode
s 的表:
id, postCode, someThing, someOtherThing
3,E8 KAK, mice, rats
4,VH3 2K2, humans, whales
另一个postCode
值重复的地方
id, postCode, someThing, someOtherThing
1,E3 4AX, cats, dogs
2,E3 4AX, elephants, sheep
到目前为止,我可以加载数据,但我不确定下一步:
myData <- read.csv("path/to/my.csv",
header=TRUE,
sep=",",
stringsAsFactors=FALSE
)
R 新手,非常感谢您的帮助。
dput
格式的数据。
df <-
structure(list(id = 1:4, postCode = structure(c(1L, 1L, 2L, 3L
), .Label = c("E3 4AX", "E8 KAK", "VH3 2K2"), class = "factor"),
someThing = structure(c(1L, 2L, 4L, 3L), .Label = c(" cats",
" elephants", " humans", " mice"), class = "factor"),
someOtherThing = structure(c(1L, 3L, 2L, 4L),
.Label = c(" dogs", " rats", " sheep", " whales "
), class = "factor")), class = "data.frame",
row.names = c(NA, -4L))
解决方案
如果 df 是您的 data.frame 的名称,它可以形成为:
df <- read.table(header = T, text = "
id, postCode, someThing, someOtherThing
1, E3 4AX, cats, dogs
2, E3 4AX, elephants, sheep
3, E8 KAK, mice, rats
4, VH3 2K2, humans, whales
")
然后可以使用n()
收集每个观察次数的函数找到唯一性和重复性grouped variable
。然后,
uniques = df %>%
group_by(postCode) %>%
filter(n() == 1)
dupes = df %>%
group_by(postCode) %>%
filter(n() > 1)
不清楚为什么有人编辑了这个回复。也许他们讨厌tribbles
推荐阅读
- php - 仅当响应页面没有自己的基本身份验证时才启用 apache HTTP 基本身份验证
- javascript - 如何解决“错误'投票'已定义但从未使用(no-unused-vars)”的问题?
- asp.net - ASP.NET GridView 不是动态填充的
- reactjs - 添加 react-redux Provider 时钩子调用无效
- javascript - 未定义 Youtube 播放器 - 多个 YT iframe 嵌入,每个都带有自定义播放按钮(未触发),使用 $.each 迭代嵌入
- reactjs - 成功调用 API 后无法更新上下文
- javascript - 机器人检查多个链接
- r - R 4.0.0:我正在尝试更改 ggplot2 中绘图的图例符号,目前我有重叠符号
- haskell - 为嵌套的单子类型实现单子实例
- mysql - 导入到 phpmyadmin 时,特定查询返回解析错误