R语言学习——常见错误、原因及解决办法
作者:互联网
1、赋值时,只能对数据类型统一的变量赋
错误:对数据框中的变量进行条件赋值时,赋值失败,显示错误为:
Error in x[...] <- m : invalid subscript type 'builtin' In addition: Warning message: In `[<-.factor`(`*tmp*`, is.na, value = 0) : invalid factor level, NA generated
原因:从excel中导入数据至数据框data.frame时,当一列变量中既有数字又有字符或逻辑值时,导入的变量数据类型会自动设置为因子factor,而factor类型的变量不能直接赋值,
解决办法:必须使用as.numeric( ),as.character( )等函数将变量的数据类型转换为“数值型”或“字符串”后,再进行赋值操作
另:对数据框中的多列变量数据批量转换数据格式时,可采用sapply和lapply函数联用的方式进行批量操作。
sapply函数输出的返回值是向量类型,可作为数据框的索引向量达到条件筛选的目的
lapply函数输出的返回值是数据框data.frame或列表list类型,可作为数据类型转换函数as.XX的嵌套函数,输出结果数据框
示例:
1、从剪切板中导入20行水质监测数据
wq <- read.table("clipboard",sep="\t",header=T)
此时查看wq数据集中的数据结构
> str(wq) 'data.frame': 20 obs. of 37 variables: $ 测站名称: Factor w/ 20 levels "坝上(龙王庙)",..: 2 17 5 4 15 13 9 18 14 11 ... $ 测站编码: int 99930128 99930133 99930129 99930130 99910136 99930134 99930131 99910124 99910126 99910125 ... $ 垂线编号: int 1 1 1 1 1 1 1 1 1 1 ... $ 层面编号: int 1 1 1 1 1 1 1 1 1 1 ... $ 水体类型: int 1 1 1 1 3 1 3 1 1 NA ... $ 采样时间: Factor w/ 1 level "2021/1/1 0:00": 1 1 1 1 1 1 1 1 1 1 ... $ WT : num 10.2 11.4 11.9 12 8.2 11 11.9 5.6 4.5 6.9 ... $ PH : num 8.2 7.7 7.7 7.7 8.1 7.7 7.6 8 8 8 ... $ DOX : num 9.4 9.5 9.6 9.7 11.7 10.1 9.2 14.1 12.8 13.2 ... $ CODMN : num 1.65 1.55 1.52 1.59 1.8 1.57 1.57 1.1 0.8 0.9 ... $ CODCR : Factor w/ 14 levels "<2.3","2.5","2.6",..: 7 13 5 13 3 11 6 10 1 1 ... $ BOD5 : Factor w/ 11 levels "<0.5","0.1","0.4",..: 1 1 1 1 8 3 2 8 7 6 ... $ NH3N : Factor w/ 14 levels "<0.025","0.022",..: 2 6 4 10 3 10 13 1 1 1 ... $ TP : Factor w/ 6 levels "<0.01","0.01",..: 1 1 1 2 3 1 1 5 4 3 ... $ TN : num 1.03 1.05 1.09 1.08 1.04 1.15 1.44 3.91 1.8 2.24 ... $ CU : Factor w/ 3 levels "<0.002","0.002",..: 1 1 1 1 1 1 1 3 2 2 ... $ ZN : Factor w/ 11 levels "<0.0006","0.0006",..: 1 1 1 10 1 1 1 3 2 6 ... $ F : num 0.15 0.21 0.13 0.19 0.13 0.19 0.2 0.19 0.13 0.12 ... $ SE : Factor w/ 1 level "<0.00041": 1 1 1 1 1 1 1 1 1 1 ... $ ARS : Factor w/ 13 levels "<0.00012","0.00016",..: 7 9 3 9 5 12 4 13 6 6 ... $ HG : Factor w/ 1 level "<0.00001": 1 1 1 1 1 1 1 1 1 1 ... $ CD : Factor w/ 1 level "<0.0005": 1 1 1 1 1 1 1 1 1 1 ... $ CR6 : Factor w/ 1 level "<0.004": 1 1 1 1 1 1 1 1 1 1 ... $ PB : Factor w/ 1 level "<0.004": 1 1 1 1 1 1 1 1 1 1 ... $ CN : Factor w/ 1 level "<0.001": 1 1 1 1 1 1 1 1 1 1 ... $ VLPH : Factor w/ 5 levels "<0.0003","0.0005",..: 1 1 1 1 1 1 1 4 2 3 ... $ OIL : Factor w/ 1 level "<0.01": 1 1 1 1 1 1 1 1 1 1 ... $ LAS : Factor w/ 1 level "<0.05": 1 1 1 1 1 1 1 1 1 1 ... $ S2 : Factor w/ 1 level "<0.005": 1 1 1 1 1 1 1 1 1 1 ... $ FCG : Factor w/ 14 levels "<10","10","100",..: 8 1 1 2 1 1 13 1 12 7 ... $ SO4 : Factor w/ 16 levels "18.41","18.66",..: 14 6 1 13 11 7 10 16 16 16 ... $ CL : Factor w/ 16 levels "2.79","2.83",..: 15 10 7 14 2 5 13 16 16 16 ... $ NO3 : Factor w/ 14 levels "0.92","0.94",..: 4 2 2 2 1 5 10 14 14 14 ... $ FE : Factor w/ 15 levels "0.0536","0.054",..: 2 5 3 1 4 5 7 15 15 15 ... $ MN : Factor w/ 14 levels "0.002","0.0035",..: 2 6 3 4 1 2 12 14 14 14 ... $ CLARITY : Factor w/ 13 levels "1","1.2","1.5",..: 9 10 11 12 6 12 8 13 13 13 ... $ CHLA : Factor w/ 11 levels "0.39","1.13",..: 5 9 7 6 10 8 3 11 11 11 ...
可以看到数据框wq中有数值型,也有因子型变量
2、将监测数据中所有的“未监测”赋值为NA
因为wq中数据有因子factor类型,所以要想达到条件赋值的目的,要分两步走,一是修改数据类型,二是赋值
(1)将数据集wq中的因子类型数据均转化为数值型
> wq[sapply(wq,is.factor)] <- lapply(wq[sapply(wq,is.factor)],as.character) > str(wq) 'data.frame': 20 obs. of 37 variables: $ 测站名称: chr "白渡滩" "香花镇张寨" "丹库中心" "仓房镇-赵沟" ... $ 测站编码: int 99930128 99930133 99930129 99930130 99910136 99930134 99930131 99910124 99910126 99910125 ... $ 垂线编号: int 1 1 1 1 1 1 1 1 1 1 ... $ 层面编号: int 1 1 1 1 1 1 1 1 1 1 ... $ 水体类型: int 1 1 1 1 3 1 3 1 1 NA ... $ 采样时间: chr "2021/1/1 0:00" "2021/1/1 0:00" "2021/1/1 0:00" "2021/1/1 0:00" ... $ WT : num 10.2 11.4 11.9 12 8.2 11 11.9 5.6 4.5 6.9 ... $ PH : num 8.2 7.7 7.7 7.7 8.1 7.7 7.6 8 8 8 ... $ DOX : num 9.4 9.5 9.6 9.7 11.7 10.1 9.2 14.1 12.8 13.2 ... $ CODMN : num 1.65 1.55 1.52 1.59 1.8 1.57 1.57 1.1 0.8 0.9 ... $ CODCR : chr "4.2" "7.2" "3.1" "7.2" ... $ BOD5 : chr "<0.5" "<0.5" "<0.5" "<0.5" ... $ NH3N : chr "0.022" "0.054" "0.045" "0.112" ... $ TP : chr "<0.01" "<0.01" "<0.01" "0.01" ... $ TN : num 1.03 1.05 1.09 1.08 1.04 1.15 1.44 3.91 1.8 2.24 ... $ CU : chr "<0.002" "<0.002" "<0.002" "<0.002" ... $ ZN : chr "<0.0006" "<0.0006" "<0.0006" "0.0028" ... $ F : num 0.15 0.21 0.13 0.19 0.13 0.19 0.2 0.19 0.13 0.12 ... $ SE : chr "<0.00041" "<0.00041" "<0.00041" "<0.00041" ... $ ARS : chr "0.00041" "0.00049" "0.00027" "0.00049" ... $ HG : chr "<0.00001" "<0.00001" "<0.00001" "<0.00001" ... $ CD : chr "<0.0005" "<0.0005" "<0.0005" "<0.0005" ... $ CR6 : chr "<0.004" "<0.004" "<0.004" "<0.004" ... $ PB : chr "<0.004" "<0.004" "<0.004" "<0.004" ... $ CN : chr "<0.001" "<0.001" "<0.001" "<0.001" ... $ VLPH : chr "<0.0003" "<0.0003" "<0.0003" "<0.0003" ... $ OIL : chr "<0.01" "<0.01" "<0.01" "<0.01" ... $ LAS : chr "<0.05" "<0.05" "<0.05" "<0.05" ... $ S2 : chr "<0.005" "<0.005" "<0.005" "<0.005" ... $ FCG : chr "30" "<10" "<10" "10" ... $ SO4 : chr "23.83" "20.27" "18.41" "22.69" ... $ CL : chr "5.66" "3.66" "3.27" "4.31" ... $ NO3 : chr "0.98" "0.94" "0.94" "0.94" ... $ FE : chr "0.054" "0.0637" "0.0547" "0.0536" ... $ MN : chr "0.0035" "0.0062" "0.0036" "0.0039" ... $ CLARITY : chr "2.7" "3" "3.7" "4" ... $ CHLA : chr "2.29" "3.05" "2.66" "2.32" ...
此时通过str( )函数看到wq数据框中的factor类型变量已经全部转换为character类型了
(2)进行条件赋值
> wq[wq=="未监测"]=NA > wq 测站名称 测站编码 垂线编号 层面编号 水体类型 采样时间 WT PH DOX CODMN CODCR BOD5 1 白渡滩 99930128 1 1 1 2021/1/1 0:00 10.2 8.2 9.4 1.65 4.2 <0.5 2 香花镇张寨 99930133 1 1 1 2021/1/1 0:00 11.4 7.7 9.5 1.55 7.2 <0.5 3 丹库中心 99930129 1 1 1 2021/1/1 0:00 11.9 7.7 9.6 1.52 3.1 <0.5 4 仓房镇-赵沟 99930130 1 1 1 2021/1/1 0:00 12.0 7.7 9.7 1.59 7.2 <0.5 5 陶岔 99910136 1 1 3 2021/1/1 0:00 8.2 8.1 11.7 1.80 2.6 0.9 6 清泉沟 99930134 1 1 1 2021/1/1 0:00 11.0 7.7 10.1 1.57 5.3 0.4 7 凉水河-台子山 99930131 1 1 3 2021/1/1 0:00 11.9 7.6 9.2 1.57 4.1 0.1 8 湘河河口 99910124 1 1 1 2021/1/1 0:00 5.6 8.0 14.1 1.10 5 0.9 9 滔河河口 99910126 1 1 1 2021/1/1 0:00 4.5 8.0 12.8 0.80 <2.3 0.8 10 淇河河口 99910125 1 1 NA 2021/1/1 0:00 6.9 8.0 13.2 0.90 <2.3 0.7 11 老鹳河河口 99910127 1 1 NA 2021/1/1 0:00 2.9 7.9 13.1 2.80 7.4 1 12 柳陂镇山跟前 99930108 1 1 NA 2021/1/1 0:00 9.1 7.9 9.5 1.49 <2.3 0.6 13 汉库中心 99930109 1 1 NA 2021/1/1 0:00 9.5 8.0 9.3 1.42 3 <0.5 14 青山-安阳 99930112 1 1 NA 2021/1/1 0:00 9.8 7.6 9.6 1.44 2.5 1.1 15 远河河口 99930114 1 1 NA 2021/1/1 0:00 10.5 7.6 9.4 1.79 <2.3 <0.5 16 武当山-三塘湾 99930117 1 1 NA 2021/1/1 0:00 12.2 7.6 9.3 1.76 <2.3 0.5 17 肖川-龙口 99930118 1 1 NA 2021/1/1 0:00 11.7 7.6 9.0 1.88 5 1.6 18 浪河口下 99930121 1 1 NA 2021/1/1 0:00 12.2 7.6 9.1 1.81 5.6 <0.5 19 坝上(龙王庙) 99930123 1 1 NA 2021/1/1 0:00 12.2 7.5 7.8 1.88 4.4 <0.5 20 白河河口(左) 99910102 1 1 NA 2021/1/1 0:00 8.4 8.2 13.4 1.50 4.7 0.8 NH3N TP TN CU ZN F SE ARS HG CD CR6 PB CN VLPH 1 0.022 <0.01 1.03 <0.002 <0.0006 0.15 <0.00041 0.00041 <0.00001 <0.0005 <0.004 <0.004 <0.001 <0.0003 2 0.054 <0.01 1.05 <0.002 <0.0006 0.21 <0.00041 0.00049 <0.00001 <0.0005 <0.004 <0.004 <0.001 <0.0003 3 0.045 <0.01 1.09 <0.002 <0.0006 0.13 <0.00041 0.00027 <0.00001 <0.0005 <0.004 <0.004 <0.001 <0.0003 4 0.112 0.01 1.08 <0.002 0.0028 0.19 <0.00041 0.00049 <0.00001 <0.0005 <0.004 <0.004 <0.001 <0.0003 5 0.04 0.02 1.04 <0.002 <0.0006 0.13 <0.00041 0.00036 <0.00001 <0.0005 <0.004 <0.004 <0.001 <0.0003 6 0.112 <0.01 1.15 <0.002 <0.0006 0.19 <0.00041 0.00095 <0.00001 <0.0005 <0.004 <0.004 <0.001 <0.0003 7 0.141 <0.01 1.44 <0.002 <0.0006 0.20 <0.00041 0.00029 <0.00001 <0.0005 <0.004 <0.004 <0.001 <0.0003 8 <0.025 0.04 3.91 0.003 0.0011 0.19 <0.00041 0.001 <0.00001 <0.0005 <0.004 <0.004 <0.001 0.001 9 <0.025 0.03 1.80 0.002 0.0006 0.13 <0.00041 0.0004 <0.00001 <0.0005 <0.004 <0.004 <0.001 0.0005 10 <0.025 0.02 2.24 0.002 0.0016 0.12 <0.00041 0.0004 <0.00001 <0.0005 <0.004 <0.004 <0.001 0.0006 11 0.118 0.08 5.94 0.003 0.002 0.17 <0.00041 0.0005 <0.00001 <0.0005 <0.004 <0.004 <0.001 0.0017 12 0.048 0.02 1.45 <0.002 0.0015 0.15 <0.00041 <0.00012 <0.00001 <0.0005 <0.004 <0.004 <0.001 <0.0003 13 0.129 0.02 1.58 <0.002 <0.0006 0.18 <0.00041 <0.00012 <0.00001 <0.0005 <0.004 <0.004 <0.001 <0.0003 14 0.1 0.02 1.46 <0.002 0.0022 0.14 <0.00041 <0.00012 <0.00001 <0.0005 <0.004 <0.004 <0.001 <0.0003 15 0.06 0.02 1.44 <0.002 <0.0006 0.21 <0.00041 <0.00012 <0.00001 <0.0005 <0.004 <0.004 <0.001 <0.0003 16 0.141 0.02 1.42 <0.002 <0.0006 0.13 <0.00041 <0.00012 <0.00001 <0.0005 <0.004 <0.004 <0.001 <0.0003 17 0.118 0.01 1.42 <0.002 <0.0006 0.11 <0.00041 <0.00012 <0.00001 <0.0005 <0.004 <0.004 <0.001 <0.0003 18 0.094 0.02 1.26 <0.002 0.0012 0.13 <0.00041 0.00016 <0.00001 <0.0005 <0.004 <0.004 <0.001 <0.0003 19 0.1 0.01 1.06 <0.002 0.0021 0.17 <0.00041 0.00044 <0.00001 <0.0005 <0.004 <0.004 <0.001 <0.0003 20 0.163 0.04 1.50 0.002 0.0041 0.15 <0.00041 0.0007 <0.00001 <0.0005 <0.004 <0.004 <0.001 0.0005 OIL LAS S2 FCG SO4 CL NO3 FE MN CLARITY CHLA 1 <0.01 <0.05 <0.005 30 23.83 5.66 0.98 0.054 0.0035 2.7 2.29 2 <0.01 <0.05 <0.005 <10 20.27 3.66 0.94 0.0637 0.0062 3 3.05 3 <0.01 <0.05 <0.005 <10 18.41 3.27 0.94 0.0547 0.0036 3.7 2.66 4 <0.01 <0.05 <0.005 10 22.69 4.31 0.94 0.0536 0.0039 4 2.32 5 <0.01 <0.05 <0.005 <10 21.63 2.83 0.92 0.0582 0.002 2 3.45 6 <0.01 <0.05 <0.005 <10 20.48 3.11 0.99 0.0637 0.0035 4 2.71 7 <0.01 <0.05 <0.005 60 21.48 4.14 1.27 0.0828 0.0131 2.5 1.53 8 <0.01 <0.05 <0.005 <10 <NA> <NA> <NA> <NA> <NA> <NA> <NA> 9 <0.01 <0.05 <0.005 52 <NA> <NA> <NA> <NA> <NA> <NA> <NA> 10 <0.01 <0.05 <0.005 20 <NA> <NA> <NA> <NA> <NA> <NA> <NA> 11 <0.01 <0.05 <0.005 390 <NA> <NA> <NA> <NA> <NA> <NA> <NA> 12 <0.01 <0.05 <0.005 40 18.95 3.69 1.3 0.067 0.0119 2.5 2.32 13 <0.01 <0.05 <0.005 110 18.66 2.86 1.39 0.1099 0.0119 2.1 2.09 14 <0.01 <0.05 <0.005 160 20.24 3.44 1.32 0.1335 0.009 1.5 1.53 15 <0.01 <0.05 <0.005 80 26.38 2.79 1.2 0.1434 0.0162 1.8 1.53 16 <0.01 <0.05 <0.005 <10 21.28 3.21 1.19 0.167 0.0081 1.2 0.39 17 <0.01 <0.05 <0.005 100 18.85 3.89 1.24 0.1158 0.0045 1.5 1.13 18 <0.01 <0.05 <0.005 40 20.52 3.28 1.13 0.1415 0.0078 1 1.13 19 <0.01 <0.05 <0.005 190 22.07 2.84 0.97 0.1034 0.0122 1.7 1.13 20 <0.01 <0.05 <0.005 380 <NA> <NA> <NA> <NA> <NA> <NA> <NA>
可以看到,所有“未监测”已经变为NA,不放心可以再做一个判断
> is.na(wq) 测站名称 测站编码 垂线编号 层面编号 水体类型 采样时间 WT PH DOX CODMN CODCR BOD5 NH3N [1,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [2,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [3,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [5,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [6,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [7,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [8,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [9,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [10,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [11,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [12,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [13,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [14,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [15,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [16,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [17,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [18,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [19,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [20,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TP TN CU ZN F SE ARS HG CD CR6 PB CN VLPH OIL LAS S2 [1,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [2,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [3,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [5,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [6,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [7,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [8,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [9,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [10,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [11,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [12,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [13,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [14,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [15,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [16,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [17,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [18,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [19,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [20,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FCG SO4 CL NO3 FE MN CLARITY CHLA [1,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [2,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [3,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [5,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [6,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [7,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [8,] FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [9,] FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [10,] FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [11,] FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [12,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [13,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [14,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [15,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [16,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [17,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [18,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [19,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [20,] FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
3、处理小于检出限(即形如:“<XX”)的数据,将此类数据全部替换为0
采用sapply()函数联用gsub()函数,批量替换文本
sapply()函数起循环遍历的作用,可以得到结构更为简单的向量vector或矩阵matrix数据集合
gsub()函数是正则表达式大礼包中用于替换所有向量中文本的函数,用于构造条件替换
> sapply(wq,gsub,pattern="<[0-9]+.?[0-9]+",replacement=0) 测站名称 测站编码 垂线编号 层面编号 水体类型 采样时间 WT PH DOX CODMN [1,] "白渡滩" "99930128" "1" "1" "1" "2021/1/1 0:00" "10.2" "8.2" "9.4" "1.65" [2,] "香花镇张寨" "99930133" "1" "1" "1" "2021/1/1 0:00" "11.4" "7.7" "9.5" "1.55" [3,] "丹库中心" "99930129" "1" "1" "1" "2021/1/1 0:00" "11.9" "7.7" "9.6" "1.52" [4,] "仓房镇-赵沟" "99930130" "1" "1" "1" "2021/1/1 0:00" "12" "7.7" "9.7" "1.59" [5,] "陶岔" "99910136" "1" "1" "3" "2021/1/1 0:00" "8.2" "8.1" "11.7" "1.8" [6,] "清泉沟" "99930134" "1" "1" "1" "2021/1/1 0:00" "11" "7.7" "10.1" "1.57" [7,] "凉水河-台子山" "99930131" "1" "1" "3" "2021/1/1 0:00" "11.9" "7.6" "9.2" "1.57" [8,] "湘河河口" "99910124" "1" "1" "1" "2021/1/1 0:00" "5.6" "8" "14.1" "1.1" [9,] "滔河河口" "99910126" "1" "1" "1" "2021/1/1 0:00" "4.5" "8" "12.8" "0.8" [10,] "淇河河口" "99910125" "1" "1" NA "2021/1/1 0:00" "6.9" "8" "13.2" "0.9" [11,] "老鹳河河口" "99910127" "1" "1" NA "2021/1/1 0:00" "2.9" "7.9" "13.1" "2.8" [12,] "柳陂镇山跟前" "99930108" "1" "1" NA "2021/1/1 0:00" "9.1" "7.9" "9.5" "1.49" [13,] "汉库中心" "99930109" "1" "1" NA "2021/1/1 0:00" "9.5" "8" "9.3" "1.42" [14,] "青山-安阳" "99930112" "1" "1" NA "2021/1/1 0:00" "9.8" "7.6" "9.6" "1.44" [15,] "远河河口" "99930114" "1" "1" NA "2021/1/1 0:00" "10.5" "7.6" "9.4" "1.79" [16,] "武当山-三塘湾" "99930117" "1" "1" NA "2021/1/1 0:00" "12.2" "7.6" "9.3" "1.76" [17,] "肖川-龙口" "99930118" "1" "1" NA "2021/1/1 0:00" "11.7" "7.6" "9" "1.88" [18,] "浪河口下" "99930121" "1" "1" NA "2021/1/1 0:00" "12.2" "7.6" "9.1" "1.81" [19,] "坝上(龙王庙)" "99930123" "1" "1" NA "2021/1/1 0:00" "12.2" "7.5" "7.8" "1.88" [20,] "白河河口(左)" "99910102" "1" "1" NA "2021/1/1 0:00" "8.4" "8.2" "13.4" "1.5" CODCR BOD5 NH3N TP TN CU ZN F SE ARS HG CD CR6 PB CN [1,] "4.2" "0" "0.022" "0" "1.03" "0" "0" "0.15" "0" "0.00041" "0" "0" "0" "0" "0" [2,] "7.2" "0" "0.054" "0" "1.05" "0" "0" "0.21" "0" "0.00049" "0" "0" "0" "0" "0" [3,] "3.1" "0" "0.045" "0" "1.09" "0" "0" "0.13" "0" "0.00027" "0" "0" "0" "0" "0" [4,] "7.2" "0" "0.112" "0.01" "1.08" "0" "0.0028" "0.19" "0" "0.00049" "0" "0" "0" "0" "0" [5,] "2.6" "0.9" "0.04" "0.02" "1.04" "0" "0" "0.13" "0" "0.00036" "0" "0" "0" "0" "0" [6,] "5.3" "0.4" "0.112" "0" "1.15" "0" "0" "0.19" "0" "0.00095" "0" "0" "0" "0" "0" [7,] "4.1" "0.1" "0.141" "0" "1.44" "0" "0" "0.2" "0" "0.00029" "0" "0" "0" "0" "0" [8,] "5" "0.9" "0" "0.04" "3.91" "0.003" "0.0011" "0.19" "0" "0.001" "0" "0" "0" "0" "0" [9,] "0" "0.8" "0" "0.03" "1.8" "0.002" "0.0006" "0.13" "0" "0.0004" "0" "0" "0" "0" "0" [10,] "0" "0.7" "0" "0.02" "2.24" "0.002" "0.0016" "0.12" "0" "0.0004" "0" "0" "0" "0" "0" [11,] "7.4" "1" "0.118" "0.08" "5.94" "0.003" "0.002" "0.17" "0" "0.0005" "0" "0" "0" "0" "0" [12,] "0" "0.6" "0.048" "0.02" "1.45" "0" "0.0015" "0.15" "0" "0" "0" "0" "0" "0" "0" [13,] "3" "0" "0.129" "0.02" "1.58" "0" "0" "0.18" "0" "0" "0" "0" "0" "0" "0" [14,] "2.5" "1.1" "0.1" "0.02" "1.46" "0" "0.0022" "0.14" "0" "0" "0" "0" "0" "0" "0" [15,] "0" "0" "0.06" "0.02" "1.44" "0" "0" "0.21" "0" "0" "0" "0" "0" "0" "0" [16,] "0" "0.5" "0.141" "0.02" "1.42" "0" "0" "0.13" "0" "0" "0" "0" "0" "0" "0" [17,] "5" "1.6" "0.118" "0.01" "1.42" "0" "0" "0.11" "0" "0" "0" "0" "0" "0" "0" [18,] "5.6" "0" "0.094" "0.02" "1.26" "0" "0.0012" "0.13" "0" "0.00016" "0" "0" "0" "0" "0" [19,] "4.4" "0" "0.1" "0.01" "1.06" "0" "0.0021" "0.17" "0" "0.00044" "0" "0" "0" "0" "0" [20,] "4.7" "0.8" "0.163" "0.04" "1.5" "0.002" "0.0041" "0.15" "0" "0.0007" "0" "0" "0" "0" "0" VLPH OIL LAS S2 FCG SO4 CL NO3 FE MN CLARITY CHLA [1,] "0" "0" "0" "0" "30" "23.83" "5.66" "0.98" "0.054" "0.0035" "2.7" "2.29" [2,] "0" "0" "0" "0" "0" "20.27" "3.66" "0.94" "0.0637" "0.0062" "3" "3.05" [3,] "0" "0" "0" "0" "0" "18.41" "3.27" "0.94" "0.0547" "0.0036" "3.7" "2.66" [4,] "0" "0" "0" "0" "10" "22.69" "4.31" "0.94" "0.0536" "0.0039" "4" "2.32" [5,] "0" "0" "0" "0" "0" "21.63" "2.83" "0.92" "0.0582" "0.002" "2" "3.45" [6,] "0" "0" "0" "0" "0" "20.48" "3.11" "0.99" "0.0637" "0.0035" "4" "2.71" [7,] "0" "0" "0" "0" "60" "21.48" "4.14" "1.27" "0.0828" "0.0131" "2.5" "1.53" [8,] "0.001" "0" "0" "0" "0" "未监测" "未监测" "未监测" "未监测" "未监测" "未监测" "未监测" [9,] "0.0005" "0" "0" "0" "52" "未监测" "未监测" "未监测" "未监测" "未监测" "未监测" "未监测" [10,] "0.0006" "0" "0" "0" "20" "未监测" "未监测" "未监测" "未监测" "未监测" "未监测" "未监测" [11,] "0.0017" "0" "0" "0" "390" "未监测" "未监测" "未监测" "未监测" "未监测" "未监测" "未监测" [12,] "0" "0" "0" "0" "40" "18.95" "3.69" "1.3" "0.067" "0.0119" "2.5" "2.32" [13,] "0" "0" "0" "0" "110" "18.66" "2.86" "1.39" "0.1099" "0.0119" "2.1" "2.09" [14,] "0" "0" "0" "0" "160" "20.24" "3.44" "1.32" "0.1335" "0.009" "1.5" "1.53" [15,] "0" "0" "0" "0" "80" "26.38" "2.79" "1.2" "0.1434" "0.0162" "1.8" "1.53" [16,] "0" "0" "0" "0" "0" "21.28" "3.21" "1.19" "0.167" "0.0081" "1.2" "0.39" [17,] "0" "0" "0" "0" "100" "18.85" "3.89" "1.24" "0.1158" "0.0045" "1.5" "1.13" [18,] "0" "0" "0" "0" "40" "20.52" "3.28" "1.13" "0.1415" "0.0078" "1" "1.13" [19,] "0" "0" "0" "0" "190" "22.07" "2.84" "0.97" "0.1034" "0.0122" "1.7" "1.13" [20,] "0.0005" "0" "0" "0" "380" "未监测" "未监测" "未监测" "未监测" "未监测" "未监测" "未监测"
标签:解决办法,FALSE,错误,...,int,常见,num,wq,TRUE 来源: https://www.cnblogs.com/foolishno1/p/15525476.html