【问题标题】:Replace one string and create a column with another替换一个字符串并用另一个创建一列
【发布时间】:2021-04-30 11:01:45
【问题描述】:

我有一个看起来像这样的数据框

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

感谢任何帮助或指导

【问题讨论】:

  • 至少你的错字是一致的 :)

标签: r string dplyr tidyverse stringr


【解决方案1】:

这样的事情可能会奏效:

 # 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)

【讨论】:

  • 抱歉所有的编辑,不得不重做几次。现在应该做你想做的事:)
【解决方案2】:

这里是 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 创建

【讨论】:

  • 请注意,如果您打算创建双打,您仍然需要调用“as.numeric”
猜你喜欢
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
  • 2011-03-25
相关资源
最近更新 更多