如果存在字符串,请用NA替换单元格

时间:2017-03-23 14:18:21

标签: r grep

我的数据框如下:

Col1    Col2     Col3     Col4    Col5
   U    N=>A       {N       A}      NA
   V      {L     E=>e        E      e}
   X    M=>P       {M       P}      NA
   Y      {Z     Q=>p        Q      p}

如何执行以下操作?

  1. 将包含=>的所有单元格替换为NA
  2. 从数据框中删除{}
  3. 看起来像这样的最终输出:

    Col1    Col2     Col3     Col4    Col5
       U      NA        N        A      NA
       V       L       NA        E       e
       X      NA        M        P      NA
       Y       Z       NA        Q       p
    

1 个答案:

答案 0 :(得分:3)

我们可以遍历列,使用grepl查找包含=>replaceNA的元素,然后使用gsub替换其他非字母字符df1[] <- lapply(df1, function(x) gsub("[{}]+", "", replace(x, grepl("=>", x), NA))) df1 # Col1 Col2 Col3 Col4 Col5 #1 U <NA> N A <NA> #2 V L <NA> E e #3 X <NA> M P <NA> #4 Y Z <NA> Q p

df1 <- structure(list(Col1 = c("U", "V", "X", "Y"), Col2 = c("N=>A", 
"{L", "M=>P", "{Z"), Col3 = c("{N", "E=>e", "{M", "Q=>p"), Col4 = c("A}", 
"E", "P}", "Q"), Col5 = c(NA, "e}", NA, "p}")), .Names = c("Col1", 
"Col2", "Col3", "Col4", "Col5"), class = "data.frame", row.names = c(NA, 
 -4L))

数据

dim dt as new datatable
'i already maked the class and now load from a database
dt=cls.getdata("select * from tblinf")
datagridview1.datasource=dt