LDT
LDT

Reputation: 3088

Replace one string and create a column with another

I have a data frame that looks like this

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

I would like to use stringr or other tidyr applications to replace "," = "." and then create a new column when there is a string like ":".

I would like my dataset to look like this:

  evindence position insertion
1        RA   24201     NA
2        RA    8915     NA
3        RA   45877     1
4        RA  251603     NA

Any help or direction are appreciated

Upvotes: 2

Views: 165

Answers (2)

tjebo
tjebo

Reputation: 23737

and here the tidyverse option. Not saying it's better. Just another option. You'll get an appropriate warning for the NAs - sometimes you want warnings.

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>

Created on 2021-01-26 by the reprex package (v0.3.0)

Upvotes: 2

thehand0
thehand0

Reputation: 1163

Something like this might do the trick :

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

Upvotes: 3

Related Questions