我的数据框如下:
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}
如何执行以下操作?
=>
的所有单元格替换为NA
。 {
和}
。 看起来像这样的最终输出:
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
答案 0 :(得分:3)
我们可以遍历列,使用grepl
查找包含=>
,replace
和NA
的元素,然后使用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