替换一个字符串并用另一个字符串创建一列
Replace one string and create a column with another
我有一个看起来像这样的数据框
position=c("24,201", "8,915", "45,877:1","251,603")
evindence=c("RA", "RA","RA","RA")
test = data.frame(evindence,position)
evindence position
1 RA 24,201
2 RA 8,915
3 RA 45,877:1
4 RA 251,603
我想使用 stringr 或其他 tidyr 应用程序来替换 "," = "."接着
当有像“:”这样的字符串时创建一个新列。
我希望我的数据集看起来像这样:
evindence position insertion
1 RA 24201 NA
2 RA 8915 NA
3 RA 45877 1
4 RA 251603 NA
感谢任何帮助或指导
这样的事情可能会成功:
# should remove the "," from the position column
test$position = gsub(",", "", position)
# should check if string contains :
test$insertion = grepl(":", test$position, fixed=TRUE)
# should extract anything before ":"
test$position = sapply(strsplit(test$position, "\:"), "[", 1)
这里是 tidyverse 选项。不是说更好。只是另一种选择。
您会收到针对 NA 的适当警告 - 有时您需要警告。
library(tidyverse)
position=c("24,201", "8,915", "45,877:1","251,603")
evindence=c("RA", "RA","RA","RA")
test = data.frame(evindence,position)
test %>%
mutate(position = str_replace(position, ",", "\.")) %>%
separate(position, c("position", "insertion"), sep = ":")
#> Warning: Expected 2 pieces. Missing pieces filled with `NA` in 3 rows [1, 2, 4].
#> evindence position insertion
#> 1 RA 24.201 <NA>
#> 2 RA 8.915 <NA>
#> 3 RA 45.877 1
#> 4 RA 251.603 <NA>
由 reprex package (v0.3.0)
创建于 2021-01-26
我有一个看起来像这样的数据框
position=c("24,201", "8,915", "45,877:1","251,603")
evindence=c("RA", "RA","RA","RA")
test = data.frame(evindence,position)
evindence position
1 RA 24,201
2 RA 8,915
3 RA 45,877:1
4 RA 251,603
我想使用 stringr 或其他 tidyr 应用程序来替换 "," = "."接着 当有像“:”这样的字符串时创建一个新列。
我希望我的数据集看起来像这样:
evindence position insertion
1 RA 24201 NA
2 RA 8915 NA
3 RA 45877 1
4 RA 251603 NA
感谢任何帮助或指导
这样的事情可能会成功:
# should remove the "," from the position column
test$position = gsub(",", "", position)
# should check if string contains :
test$insertion = grepl(":", test$position, fixed=TRUE)
# should extract anything before ":"
test$position = sapply(strsplit(test$position, "\:"), "[", 1)
这里是 tidyverse 选项。不是说更好。只是另一种选择。 您会收到针对 NA 的适当警告 - 有时您需要警告。
library(tidyverse)
position=c("24,201", "8,915", "45,877:1","251,603")
evindence=c("RA", "RA","RA","RA")
test = data.frame(evindence,position)
test %>%
mutate(position = str_replace(position, ",", "\.")) %>%
separate(position, c("position", "insertion"), sep = ":")
#> Warning: Expected 2 pieces. Missing pieces filled with `NA` in 3 rows [1, 2, 4].
#> evindence position insertion
#> 1 RA 24.201 <NA>
#> 2 RA 8.915 <NA>
#> 3 RA 45.877 1
#> 4 RA 251.603 <NA>
由 reprex package (v0.3.0)
创建于 2021-01-26