Replace all occurrences of a string in a data frame

后端 未结 6 687
面向向阳花
面向向阳花 2020-11-27 02:54

I\'m working on a data frame that has non-detects which are coded with \'<\'. Sometimes there is a space after the \'<\' and sometimes not e.g. \'<2\' or \'< 2\

6条回答
  •  粉色の甜心
    2020-11-27 03:19

    If you are only looking to replace all occurrences of "< " (with space) with "<" (no space), then you can do an lapply over the data frame, with a gsub for replacement:

    > data <- data.frame(lapply(data, function(x) {
    +                  gsub("< ", "<", x)
    +              }))
    > data
      name var1 var2
    1    a   <2   <3
    2    a   <2   <3
    3    a   <2   <3
    4    b   <2   <3
    5    b   <2   <3
    6    b   <2   <3
    7    c   <2   <3
    8    c   <2   <3
    9    c   <2   <3
    

提交回复
热议问题