如何从每一行中删除相似的字符串

时间:2019-04-10 16:10:46

标签: r

我有这样的数据

df<- structure(list(Name = structure(c(1L, 18L, 19L, 17L, 4L, 5L, 
10L, 11L, 14L, 15L, 3L, 6L, 9L, 12L, 13L, 16L, 2L, 7L, 8L), .Label = c("J11A427M21_rep1_a", 
"J11DFCd32M21_rep1_c", "J11DFOKI24M21_rep1_a", "J11JENDE6M21_rep1_a", 
"J12BIpedar6M21_rep2_a", "J12DFC24M21_rep2_a", "J12DI032M21_rep2_c", 
"J13D032M21_rep3_c", "J13DFCIO4M21_rep3_a", "J13SAG6M21_rep3_a", 
"J21CALU6M21_rep1_b", "J21DFC24M21_rep1_b", "J22CI024M21_rep2_b", 
"J22CoS6M21_rep2_b", "J23CNAN6M21_rep3_b", "J23DF024M21_rep3_b", 
"J23MADAR3M21_rep3_d", "J31CIRE1M21_rep1_c", "J32Khar1M21_rep2_c"
), class = "factor"), value1 = c(1L, 2L, 3L, 3L, NA, 23L, 5L, 
345L, 134L, 3L, 6L, 56L, 45L, 7L, 46L, 88L, 3L, 8L, 9L), value2 = c(3L, 
32L, 4L, 2L, 2L, 5L, 346L, 3L, 645L, 7L, 67L, 56L, NA, 678L, 
76L, 8L, 8L, NA, 5L)), class = "data.frame", row.names = c(NA, 
-19L))

我想从每一行中删除一部分字符串,并将其放在另一行中 我想从字符串中删除 M21 并将其保存在另一列中 所以愿望输出就是这样

drout<- structure(list(Name1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "M21", class = "factor"), 
    Name = structure(c(1L, 18L, 19L, 17L, 4L, 5L, 10L, 11L, 14L, 
    15L, 3L, 6L, 9L, 12L, 13L, 16L, 2L, 7L, 8L), .Label = c("J11A427_rep1_a", 
    "J11DFCd32_rep1_c", "J11DFOKI24_rep1_a", "J11JENDE6_rep1_a", 
    "J12BIpedar6_rep2_a", "J12DFC24_rep2_a", "J12DI032_rep2_c", 
    "J13D032_rep3_c", "J13DFCIO4_rep3_a", "J13SAG6_rep3_a", "J21CALU6_rep1_b", 
    "J21DFC24_rep1_b", "J22CI024_rep2_b", "J22CoS6_rep2_b", "J23CNAN6_rep3_b", 
    "J23DF024_rep3_b", "J23MADAR3_rep3_d", "J31CIRE1_rep1_c", 
    "J32Khar1_rep2_c"), class = "factor"), value1 = c(1L, 2L, 
    3L, 3L, NA, 23L, 5L, 345L, 134L, 3L, 6L, 56L, 45L, 7L, 46L, 
    88L, 3L, 8L, 9L), value2 = c(3L, 32L, 4L, 2L, 2L, 5L, 346L, 
    3L, 645L, 7L, 67L, 56L, NA, 678L, 76L, 8L, 8L, NA, 5L)), class = "data.frame", row.names = c(NA, 
-19L))

2 个答案:

答案 0 :(得分:1)

# base approach
df$Name1 = ifelse(grepl("M21", df$Name, fixed = TRUE), "M21", NA)
df$Name = sub(pattern = "M21", replacement = "", df$Name, fixed = TRUE)

# stringr approach (reset data if you just ran base approach)
library(stringr)
df$Name1 = str_extract(df$Name, fixed("M21"))
df$Name = str_replace(df$Name, pattern = fixed("M21"), replacement = "")

答案 1 :(得分:1)

通过使用 tidyverse 包,您可以

ddf <- df %>% mutate(Name = str_remove(Name, 'M21')) %>% 
              mutate(Name1 = rep('M21', count(df))) %>%
              select(Name1, Name, value1, value2)