r - 根据数据的可用性创建重复行
问题描述
我试图用分隔符分割一列,在这种情况下是逗号,然后根据逗号之间的元素数量制作重复的行。我使用了 str_split_fixed() 它将适当地拆分列,但我不知道如何使用它来制作重复的行。
例如,我有这个数据框:
V1 V2 V3 V4
foo a b 1,2,3
bar c d 1,2
并希望将其作为输出发送到此数据框:
V1 V2 V3 V4
foo a b 1
foo a b 2
foo a b 3
bar c d 1
bar c d 2
解决方案
为此,有separate_rows
library(tidyverse)
df1 %>%
separate_rows(V4, sep=",", convert = TRUE)
#. V1 V2 V3 V4
#1 foo a b 1
#2 foo a b 2
#3 foo a b 3
#4 bar c d 1
#5 bar c d 2
数据
df1 <- structure(list(V1 = c("foo", "bar"), V2 = c("a", "c"), V3 = c("b",
"d"), V4 = c("1,2,3", "1,2")), class = "data.frame", row.names = c(NA,
-2L))
推荐阅读
- c# - 禁用敌人,获取随机数,传送到该数字作为坐标,启用敌人
- reactjs - 使用钩子将类组件转换为函数式
- c# - Unity 输入系统鼠标按键按住并拖动
- javascript - 如何解决屏幕缩小时出现的内容重定位问题?
- css - ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。在 mat-tab-group 上使用 Ngclass
- r - 以 yymm 格式将月份添加到日期
- sql - 当 unpivot 未检测到具有不同类型的字段时,雪花如何转换选择查询的所有字段?
- java - 使用信号量开发同步算法
- python - 多次运行脚本时出现 Python 错误:模块“__main__”没有属性“__spec__”
- jenkins - Jenkins - 通过证书进行代理身份验证