我正在尝试使用read.csv
读取一些数据第3-6列有数字,有些单元格包含'失败',就像NA一样。因此,它们导入为'chr'而不是'num'。什么样的函数可以删除失败并转换为num?
-Justin 的修改
> dput(data[3:6])
structure(list(Min.Peak...pA. = c(-71.38548753, -1848.248019,
-4408.230552, -4095.830695, -907.5675435, -10361.98606, -11684.40117,
-19168.64772, -52.88647531, -1910.259488, -3092.095923, -3242.998666,
-68.70519556, -3335.389339, -6721.068048, -14260.93203, -66.44258505,
-1410.137842, -2496.544791, -9608.0219, -110.8242604, -1604.481171,
-4970.15043, -7411.813595, -75.65103562, -4820.455709, -5797.556001,
-7876.191933, -52.71190168, -19.23272088, -4115.089946, -11028.89694,
-37.78640405, -2123.881088, -3695.938087, -7013.299182, -183.1306058,
-3961.701871, -4864.627676, -15106.50731, -255.772376, -8882.731708,
-11013.93003, -19539.43774, -42.41891994, -2531.702538, -4868.278045,
-6315.470197, -1180.048764, -6046.513339, -4965.390819, -7817.293102,
-50.82197879, -3243.366283, -5427.94134, -13410.84216, -147.2783562,
-7370.998499, -7853.193502, -13069.53797, -341.4078982, -9169.565123,
-5551.095603, -12040.11667, -108.662436, -8496.266155, -6849.994739,
-9249.801087, -121.5890729, -6237.589723, -6280.46217, -15325.14297,
-23.46366005, -1064.396942, -1810.256924, -6287.549874, -91.33056117,
-6448.080323, -5382.794949, -10214.03893, -259.7761914, -11328.83209,
-8148.28781, -11281.60005, -69.21085227, -7141.160185, -6645.019468,
-22210.46238, -39.80361239, -2689.475457, -4273.815323, -9599.694753,
-203.1170062, -3573.172524, -5976.138075, -8135.222652, -86.31915283,
-5316.793225, -5674.512236, -7145.048435, -893.2347787, -1509.258764,
-5977.742747, -67.20436935, -1750.403459, -4214.563669, -6683.358662,
-85.73375733, -6690.805312, -8279.023166, -8147.564739, -40.77643648,
-2178.024142, -3536.301302, -8110.99565, -252.0816632, -10115.81761,
-4815.289668, -12376.98256, -33.93513595, -481.4547413, -729.605423,
-2384.650283, -178.3966193, -10306.8118, -9392.2647, -19713.179,
-234.748083, -8623.60283, -6429.528802, -19582.55665, -170.4701194,
-7292.176345, -6429.963841, -5886.854494, -169.2131981, -1710.463411,
-3354.651653, -8591.095081, -19.19041195, -104.4811318, -225.3096515,
-868.54383, -122.0535731, -4772.788568, -3795.654471, -14125.76093,
-243.7615988, -5324.821092, -4847.835139, -7775.784257, -296.2380184,
-5151.289266, -4887.573798, -11851.64177, -53.62334925, -2854.907114,
-5353.755189, -11877.21437, -115.4757167, -8165.479208, -7119.821798,
-22815.30955), Avg.Steady.State...pA. = c(-156.4558338, -207.2516972,
-499.3118886, -611.4115357, -198.4419088, -1680.612107, -728.3219423,
-5320.917133, 2.854732323, -428.4412391, -487.27141, -706.1985758,
-299.8769296, -1228.182103, -1923.784826, -2349.930178, 65.65542676,
-83.20469617, -405.6263758, -180.5612591, -210.2366808, -692.2184696,
-928.0884747, -1113.878315, -248.543373, -1004.219522, -1152.42882,
-626.3266358, -29.80703294, -488.8464959, -153.7905348, -1201.163132,
-38.59532925, -427.8320732, -463.8880624, -1375.298721, -451.1947046,
-732.7102383, -806.2554719, -4110.12958, -411.057644, -963.7211819,
-1077.721728, -3891.312356, -232.9505687, -509.7634789, -1175.864244,
-909.2495489, -456.484994, -811.7198073, -1375.610103, -1399.872594,
-315.7290322, -853.100816, -1244.000175, -843.4292395, -848.5864592,
-1665.780647, -2701.243974, -5807.662046, -892.5183997, -328.7659649,
-148.0183088, -1152.823193, -783.5360085, -875.7061919, -1162.307758,
-654.1160127, -577.2286295, -1628.28912, -1722.207308, -1499.411145,
-37.62480842, -219.3052317, -227.8011128, -315.750221, -772.5637899,
-549.3143563, -1796.766019, -774.0635032, -586.6651232, -1225.352473,
-2248.738877, -2448.490062, -537.2006376, -805.6454265, -935.7462757,
-4281.463308, -65.80110365, -568.879472, -588.1439001, -1090.306794,
-338.8084558, -435.2948444, -495.9746094, -446.6795105, -229.6983763,
-213.1368609, -200.6713348, -202.7463225, -693.8336815, -774.1368082,
-1112.564985, -115.4592308, -590.5487398, -461.8749613, -705.9548638,
-435.7112024, -1250.185417, -1755.07252, -1984.506838, -161.9784544,
-707.8499085, -781.9936258, -560.687719, -1621.864557, -1375.350521,
-1382.285636, -3585.427784, -44.31893406, -220.8694939, -314.1272518,
-520.1850595, -1382.100044, -1174.108637, -1888.971054, -3091.606361,
-698.0928417, -613.8486451, -908.7226178, -2989.443015, -376.7857118,
-1037.408121, -776.1034398, -855.4597697, -88.03316633, -254.8773797,
-356.6256283, -1103.217741, -22.98111061, -205.4697556, -428.0938051,
-780.000863, -647.634804, -628.693487, -848.6049318, -1233.441735,
-929.9300305, -827.689611, -1329.917387, -1194.051134, -680.0114336,
-706.6014541, -1005.709168, -729.671711, -109.7952491, -661.9612887,
-497.9392714, -878.4510045, -799.6333055, -1493.093933, -2238.444924,
-6141.637188), X1.C.Tau1...ms. = c("Failed", "2103.162699", "654.2568125",
"515.0865465", "Failed", "471.0639716", "355.8718751", "801.3492494",
"Failed", "1042.782008", "988.3702197", "671.4573022", "Failed",
"1896.790759", "716.4795246", "914.104118", "Failed", "201.2439353",
"203.4978666", "329.545244", "6190.334187", "479.7633387", "682.8113498",
"939.185729", "Failed", "1487.902777", "777.4115409", "433.4616754",
"Failed", "5707.427648", "352.4757288", "1020.463432", "8257.499681",
"230.3492956", "203.8221935", "331.6711501", "Failed", "1320.966905",
"558.319266", "830.4531079", "Failed", "698.2464232", "617.065896",
"578.260274", "Failed", "3131.139472", "745.815039", "479.7399288",
"37.87940657", "3879.029494", "452.1720195", "1626.106836", "Failed",
"1436.912256", "637.3812591", "735.5310673", "Failed", "7238.687806",
"738.3835667", "1492.482161", "14616.33978", "857.2685963", "312.0372646",
"429.652111", "Failed", "1254.38919", "741.1808766", "418.8582914",
"Failed", "1118.618221", "646.3542054", "728.1936382", "Failed",
"354.4949485", "528.2566664", "391.7224484", "Failed", "2436.098245",
"693.9700045", "1327.950996", "16931.94683", "711.7718939", "575.6593833",
"2379.349827", "Failed", "2340.002461", "877.7275317", "911.7213517",
"Failed", "366.879194", "451.0460688", "490.9762532", "Failed",
"872.9814367", "460.6578144", "438.8290913", "46918.42883", "312.9269143",
"265.6175109", "229.5357985", "317.7740461", "372.1306909", "1075.018636",
"Failed", "452.8186552", "751.2654075", "1091.377277", "Failed",
"2097.087637", "711.7215388", "639.0353856", "Failed", "5781.189249",
"1712.681571", "832.8932007", "Failed", "2999.046275", "553.7986161",
"1864.580424", "Failed", "649.9956982", "754.1327368", "1609.747416",
"Failed", "1667.689252", "539.289796", "1070.624299", "6440.002417",
"1306.169065", "586.7839641", "1124.924567", "Failed", "810.5778161",
"662.9696298", "516.1097592", "Failed", "302.5306095", "242.3987567",
"210.1155333", "862.9695628", "1182.400621", "5336.318578", "219.6511422",
"Failed", "2467.789512", "755.1769838", "733.5672614", "Failed",
"4178.620679", "705.1594617", "2324.44184", "Failed", "1866.047214",
"802.7889146", "772.8803334", "Failed", "621.541695", "853.9160071",
"541.2206178", "Failed", "2184.724427", "963.0741524", "1252.016613"
), X1.C.Tau2...ms. = c("362.6424851", "310.4002171", "272.7280065",
"267.0628241", "403.8748772", "295.4998917", "567.4688738", "527.2612417",
"512.5393577", "340.7312903", "297.306266", "331.3522423", "319.1897935",
"236.154893", "231.3681131", "Failed", "18.31947625", "607.8691629",
"477.5053805", "1986.169436", "398.188174", "296.4246039", "237.2923057",
"419.5304484", "371.8110192", "322.1339429", "313.1678621", "556.7348115",
"376.8746295", "311.2900424", "954.9782969", "300.403217", "434.2989573",
"317.4822155", "395.3068772", "257.1152821", "357.5807867", "319.0041734",
"373.4139263", "362.7700412", "370.1032719", "286.4080528", "312.1974908",
"489.4459462", "404.0667199", "324.6393623", "313.9882992", "371.0687421",
"292.2568272", "201.0349308", "292.1833965", "142.9019925", "233.7636561",
"242.6127532", "204.7367679", "245.368056", "465.0546307", "357.9163358",
"390.1084203", "473.7381155", "323.4341699", "305.5230954", "462.1336864",
"283.5710827", "420.8480856", "Failed", "367.0565055", "684.0806809",
"325.5621286", "255.1640322", "262.076803", "305.4641121", "360.831728",
"373.1757708", "398.9665742", "553.9133026", "352.5631994", "335.3614802",
"288.7037509", "418.8665834", "213.3576967", "213.2156677", "263.132751",
"171.362135", "347.8847556", "Failed", "329.2919785", "382.9287825",
"395.2422887", "315.9677061", "310.6099621", "416.6914405", "372.5723859",
"271.0784373", "274.5547822", "343.4086455", "297.2074641", "344.6087065",
"335.1843911", "244.767655", "236.4569494", "258.161804", "212.327945",
"343.5433798", "264.6451911", "281.1233835", "307.653004", "327.0864195",
"352.8554575", "324.5163249", "438.9511041", "289.4722694", "275.2576084",
"380.5508973", "312.0007152", "267.4527886", "232.9193523", "277.3619503",
"245.8463796", "327.5935273", "316.7834723", "335.7957262", "374.5701331",
"383.2666041", "352.4589618", "379.9762537", "337.506651", "362.9750526",
"266.0720612", "339.2840943", "385.0816829", "397.6421166", "383.6366896",
"391.9358643", "412.6495374", "1050.554291", "681.18221", "716.7991053",
"1247.166382", "1308.618453", "476.4643091", "496.763805", "442.8319301",
"291.8850404", "288.3804696", "255.3793404", "274.231915", "336.644858",
"310.7717616", "275.7836938", "350.7166505", "387.0430532", "327.4122634",
"313.6584658", "415.535093", "608.0929732", "395.9465642", "373.4319836",
"545.5296663", "356.0077187", "287.0571824", "329.7159136", "439.2875597"
)), .Names = c("Min.Peak...pA.", "Avg.Steady.State...pA.", "X1.C.Tau1...ms.",
"X1.C.Tau2...ms."), row.names = c(NA, -163L), class = "data.frame")
以下是有问题的3列。因为那里的失败,它们导入为Char而不是num。我怎样才能删除'失败'并替换为什么,之后我应该能够使用as.numeric(),对吗?
答案 0 :(得分:1)
我们可以先尝试将"Failed"
替换为NA
,然后将3:6
列转换为数字:
data[,3:6][data[,3:6] == "Failed" ] <- NA
data[,3:6] <- sapply(data[3:6],as.numeric)