使用 gsub() 从每行多个项目的列表中提取唯一数字?
Extract unique numbers from a list with multiple items per line using gsub()?
列表 A1 的结构如下:
#[[1]]
[1] "12" "1"
#[[2]]
[1] "13" "1"
#[[3]]
[1] "14" "2"
#[[4]]
[1] "15" "2"
#[[5]]
[1] "16" "1"
#[[6]]
[1] "18" "2"
#[[7]]
[1] "20" "0"
#[[8]]
[1] "21" "2"
#[[9]]
[1] "21" "4"
#[[10]]
[1] "34" "1" "0"
#[[11]]
[1] "42" "1" "1"
#[[12]]
[1] "42" "2" "2"
并且我正在尝试提取每个列表元素的第一项中的唯一值(例如,[[1]] 中的数字 12)。
当我使用
v <- unique(gsub( " .*$", "", A1))
生成的向量 v 看起来像
"c("12"," "c("13"," "c("14"," "c("15"," "c("16"," "c("18"," "c("20"," "c("21","
"c("34"," "c("42","
期望的结果是 (12, 13, 14, 15, 16, 17, 18, 20, 21, 34, 42)
如何去掉额外的字符,它们从哪里来?
提前致谢!
您可以使用
v <- list(c("12", "1"), c("13", "1"), c("12", "3"))
unique(sapply(v, "[[", 1))
# => [1] "12" "13"
注:
sapply(v, "[[", 1)
- 获取第一个项目
unique
只保留唯一值。
列表 A1 的结构如下:
#[[1]] [1] "12" "1"
#[[2]] [1] "13" "1"
#[[3]] [1] "14" "2"
#[[4]] [1] "15" "2"
#[[5]] [1] "16" "1"
#[[6]] [1] "18" "2"
#[[7]] [1] "20" "0"
#[[8]] [1] "21" "2"
#[[9]] [1] "21" "4"
#[[10]] [1] "34" "1" "0"
#[[11]] [1] "42" "1" "1"
#[[12]] [1] "42" "2" "2"
并且我正在尝试提取每个列表元素的第一项中的唯一值(例如,[[1]] 中的数字 12)。
当我使用
v <- unique(gsub( " .*$", "", A1))
生成的向量 v 看起来像
"c("12"," "c("13"," "c("14"," "c("15"," "c("16"," "c("18"," "c("20"," "c("21"," "c("34"," "c("42","
期望的结果是 (12, 13, 14, 15, 16, 17, 18, 20, 21, 34, 42)
如何去掉额外的字符,它们从哪里来?
提前致谢!
您可以使用
v <- list(c("12", "1"), c("13", "1"), c("12", "3"))
unique(sapply(v, "[[", 1))
# => [1] "12" "13"
注:
sapply(v, "[[", 1)
- 获取第一个项目unique
只保留唯一值。