【发布时间】:2021-07-21 05:02:09
【问题描述】:
我正在尝试通过部分字符串匹配来子集数据帧。拆分和比较也可以工作,因为字符串可以用“|”拆分 我相信我在过去的类似案例中使用了 %in% ,但它对此不起作用。 有什么建议吗?
df <- read.table(text="
col1 cOL2
1 '2.16.840.1.113883.10.20.22.4.19 | 2.16.840.1.113883.10.20.22.4.19 | 2.16.840.1.113883.10.20.1.47'
2 '2.16.840.1.113883.10.20.22.4.4 | 2.16.840.1.113883.10.20.22.4.4 | 2.16.840.1.113883.10.20.22.4.64'
3 '2.16.840.1.113883.10.20.22.4.64 | 2.16.840.1.113883.10.20.22.4.78 | 2.16.840.1.113883.10.20.1.47'
4 '2.16.840.1.113883.10.20.22.4.19 | 2.16.840.1.113883.10.20.22.4.19 | 2.16.840.1.113883.10.20.1.47'
5 '2.16.840.1.113883.10.20.22.4.19 | 2.16.840.1.113883.10.20.22.4.19 | 2.16.840.1.113883.10.20.1.47'
", header=T, stringsAsFactors=FALSE)
df[which(df$cOL2 == 1 & df$cOL2 %in% '2.16.840.1.113883.10.20.22.4.19' ),]
【问题讨论】:
标签: r dataframe compare subset string-matching