除了一个,R在所有列中用0替换NA

时间:2017-02-17 22:37:47

标签: r dplyr

我正在尝试将数据框中的所有NA替换为0。 R对除AMATAWt以外的所有列执行此操作。请看看为什么会发生这种情况以及如何解决。谢谢。

> dput(head(StackedYr3, 20))
structure(list(Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L), .Label = c("12-Sep-16", 
"13-Sep-16", "14-Sep-16", "15-Sep-16", "26-Sep-16", "27-Sep-16", 
"29-Sep-16", "30-Sep-16", "8-Sep-16", "9-Sep-16"), class = "factor"), 
    Plot = c(12L, 12L, 15L, 15L, 19L, 19L, 21L, 21L, 26L, 26L, 
    27L, 27L, 31L, 31L, 35L, 35L, 36L, 36L, 43L, 43L), Side = structure(c(2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 2L, 1L), .Label = c("E", "W"), class = "factor"), 
    Crop = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("alfalfa", 
    "corn", "oat", "soybean"), class = "factor"), Rotation.treatment = structure(c(9L, 
    9L, 8L, 8L, 7L, 7L, 9L, 9L, 8L, 8L, 7L, 7L, 9L, 9L, 7L, 7L, 
    8L, 8L, 9L, 9L), .Label = c("A4", "C2", "C3", "C4", "O3", 
    "O4", "S2", "S3", "S4"), class = "factor"), Herbicide.treatment = structure(c(1L, 
    2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 
    2L, 1L, 1L, 2L), .Label = c("conv", "low"), class = "factor"), 
    DIGSANo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 2, 0, 0, 0), DIGSAWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 1.2, 0, 0, 0), SETFANo = c(5, 35, 0, 18, 
    30, 0, 2, 0, 0, 32, 0, 0, 5, 6, 1, 0, 0, 0, 9, 2), SETFAWt = c(7.19, 
    14.29, 0, 118.49, 69.93, 0, 12.92, 0, 0, 175.68, 0, 0, 2.93, 
    4.44, 0.45, 0, 0, 0, 8.06, 5.65), SETLUNo = c(3, 0, 3, 5, 
    1, 9, 2, 1, 0, 5, 1, 0, 1, 2, 2, 2, 2, 0, 4, 8), SETLUWt = c(0.84, 
    0, 1.84, 5.12, 0.56, 5.82, 7.81, 0.83, 0, 27.6, 0.01, 0, 
    0.18, 4.82, 1.62, 6.98, 0.93, 0, 1.39, 20.16), PANCANO = c(0, 
    0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0), 
    PANCAWt = c(0, 0, 0, 0, 6.91, 0, 0, 0, 0, 0, 0, 0.65, 0, 
    0, 0, 0, 0, 0, 0, 0), ECHCGNo = c(0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), ECHCGWt = c(0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), ERBVINo = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0), 
    ERBVIWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4.36, 0, 0, 
    0, 0, 0, 0, 0, 0), CYPESNo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), CYPESWt = c(0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), AMATANo = c(2, 
    0, 1, 1, 23, 5, 0, 2, 1, 4, 3, 10, 4, 7, 0, 2, 16, 0, 3, 
    0), AMATAWt = structure(c(4L, 1L, 3L, 36L, 46L, 25L, 1L, 
    53L, 11L, 26L, 51L, 29L, 42L, 28L, 1L, 54L, 49L, 1L, 35L, 
    1L), .Label = c("", "-", "0.01", "0.03", "0.13", "0.17", 
    "0.19", "0.3", "0.4", "0.45", "0.65", "0.66", "0.7", "0.72", 
    "0.78", "1.01", "1.32", "1.36", "1.38", "1.61", "1.63", "1.68", 
    "1.74", "1.94", "103.68", "11.04", "13.28", "151.68", "151.73", 
    "154.98", "16.61", "2.47", "2.82", "2.88", "21.29", "23.36", 
    "23.57", "24.45", "3.07", "3.55", "31.25", "38.86", "39.42", 
    "4.11", "42.93", "422.45", "5.05", "5.39", "551.11", "61.05", 
    "65.37", "73.22", "88.87", "97.67"), class = "factor"), CHEALNo = c(2, 
    9, 1, 19, 13, 7, 35, 2, 2, 6, 1, 19, 2, 4, 0, 0, 47, 2, 11, 
    4), CHEALWt = c(1.7, 60.01, 0.01, 119.63, 71.42, 14.5, 370.82, 
    20.04, 0.35, 34.6, 0.77, 80.88, 4.74, 79.6, 0, 0, 368.42, 
    0.74, 26.61, 1.34), SOLPTNo = c(0, 0, 0, 1, 0, 0, 0, 0, 1, 
    0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0), SOLPTWt = c(0, 0, 0, 0.05, 
    0, 0, 0, 0, 1.2, 0, 0, 0, 0.12, 0, 0, 0, 0, 0, 0, 0), PHYSUNo = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    PHYSUWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), POLCCNo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), POLCCWt = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), POLPYNo = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0), 
    POLPYWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 2.74, 0, 0, 0), ABUTHNo = c(0, 0, 0, 0, 2, 0, 1, 0, 3, 
    0, 2, 0, 4, 0, 1, 1, 3, 0, 0, 1), ABUTHWt = c(0, 0, 0, 0, 
    46.75, 0, 120.17, 0, 13.86, 0, 4.67, 0, 281.75, 0, 0.87, 
    94.27, 36.99, 0, 0, 0.2), TAROFNo = c(0, 0, 4, 0, 0, 1, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), TAROFWt = c(0, 0, 
    0.84, 0, 0, 0.37, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0), EPHHTNo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0), EPHHTWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), CIRARNo = c(0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), CIRARWt = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    SONARNo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), SONARWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), MORALNo = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), MORALWt = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    OXASTNo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), OXASTWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), POLLANo = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), POLLAWt = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    ASCSYNo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), ASCSYWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), POLAVNo = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), POLAVWt = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    PLAMANo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), PLAMAWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), PANVINo = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), PANVIWt = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    PANDINo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 1, 0), PANDIWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0.29, 0), CONARNo = c(0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), CONARWt = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    BRJANo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), BRJAWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), FCHGHNo = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), FCHGHWt = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    IPOHENo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), IPOHEWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), RUMCRNo = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), RUMCRWt = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    EPHMANo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), EPHMAWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), ELYREPNo = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), ELYREPWt = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    POATRNo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), POATRWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), SOLNINo = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), SOLNIWt = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    ASTPINo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), ASTPIWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Maple.TreeNo = c(0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Maple.TreeWt = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    Mulberry.TreeNo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0), Mulberry.TreeWt = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), BROTENo = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    BROTEWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), POAPRANo = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), POAPRAWt = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), OATSNo = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    OATSWt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), Wt = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0), No = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Wt.1 = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), TOTALNo = c(12L, 
    44L, 9L, 44L, 71L, 22L, 40L, 5L, 7L, 47L, 7L, 35L, 17L, 19L, 
    4L, 5L, 72L, 2L, 28L, 15L), TOTALWt = c(9.76, 74.3, 2.7, 
    266.65, 618.02, 124.37, 511.72, 109.74, 16.06, 248.92, 70.82, 
    237.62, 328.58, 240.54, 2.94, 198.92, 961.39, 0.74, 57.64, 
    27.35), sample.area..m.2. = c(18.532, 18.532, 18.532, 18.532, 
    18.532, 18.532, 18.532, 18.532, 18.532, 18.532, 18.532, 18.532, 
    18.532, 18.532, 18.532, 18.532, 18.532, 18.532, 18.532, 18.532
    ), g.m.2 = c(0.53, 4.01, 0.15, 14.39, 33.35, 6.71, 27.61, 
    5.92, 0.87, 13.43, 3.82, 12.82, 17.73, 12.98, 0.16, 10.73, 
    51.88, 0.04, 3.11, 1.48), kg.ha = c(5.27, 40.09, 1.46, 143.89, 
    333.49, 67.11, 276.13, 59.22, 8.67, 134.32, 38.21, 128.22, 
    177.3, 129.8, 1.59, 107.34, 518.77, 0.4, 31.1, 14.76), lb.acre = c(4.7, 
    35.8, 1.3, 128.49, 297.8, 59.93, 246.58, 52.88, 7.74, 119.95, 
    34.13, 114.5, 158.33, 115.91, 1.42, 95.85, 463.26, 0.36, 
    27.77, 13.18), plants.m2 = c(0.65, 2.37, 0.49, 2.37, 3.83, 
    1.19, 2.16, 0.27, 0.38, 2.54, 0.38, 1.89, 0.92, 1.03, 0.22, 
    0.27, 3.89, 0.11, 1.51, 0.81)), .Names = c("Date", "Plot", 
"Side", "Crop", "Rotation.treatment", "Herbicide.treatment", 
"DIGSANo", "DIGSAWt", "SETFANo", "SETFAWt", "SETLUNo", "SETLUWt", 
"PANCANO", "PANCAWt", "ECHCGNo", "ECHCGWt", "ERBVINo", "ERBVIWt", 
"CYPESNo", "CYPESWt", "AMATANo", "AMATAWt", "CHEALNo", "CHEALWt", 
"SOLPTNo", "SOLPTWt", "PHYSUNo", "PHYSUWt", "POLCCNo", "POLCCWt", 
"POLPYNo", "POLPYWt", "ABUTHNo", "ABUTHWt", "TAROFNo", "TAROFWt", 
"EPHHTNo", "EPHHTWt", "CIRARNo", "CIRARWt", "SONARNo", "SONARWt", 
"MORALNo", "MORALWt", "OXASTNo", "OXASTWt", "POLLANo", "POLLAWt", 
"ASCSYNo", "ASCSYWt", "POLAVNo", "POLAVWt", "PLAMANo", "PLAMAWt", 
"PANVINo", "PANVIWt", "PANDINo", "PANDIWt", "CONARNo", "CONARWt", 
"BRJANo", "BRJAWt", "FCHGHNo", "FCHGHWt", "IPOHENo", "IPOHEWt", 
"RUMCRNo", "RUMCRWt", "EPHMANo", "EPHMAWt", "ELYREPNo", "ELYREPWt", 
"POATRNo", "POATRWt", "SOLNINo", "SOLNIWt", "ASTPINo", "ASTPIWt", 
"Maple.TreeNo", "Maple.TreeWt", "Mulberry.TreeNo", "Mulberry.TreeWt", 
"BROTENo", "BROTEWt", "POAPRANo", "POAPRAWt", "OATSNo", "OATSWt", 
"Wt", "No", "Wt.1", "TOTALNo", "TOTALWt", "sample.area..m.2.", 
"g.m.2", "kg.ha", "lb.acre", "plants.m2"), row.names = c(NA, 
20L), class = "data.frame")

和我使用的代码

library(dplyr)
library(tidyverse)

as.matrix(StackedYr3)
StackedYr3[is.na(StackedYr3)] <- 0

感谢Sotos指出那些未被替换的单元格是空的而不是NA。然后我用

StackedYr3$AMATAWt[StackedYr3$AMATAWt==""]<-0

但是出现了这个错误

Warning message:In [<-.factor*tmp*,StackedYr3 $ AMATAWt ==“”,值= c(4L,:无效因子级别,NA生成`

空单元现在被NA替换,但这些NA不能用0替换。将数据框转换为矩阵无济于事。

1 个答案:

答案 0 :(得分:0)

事实证明AMATAWt是非数字列。 as.numeric完成了这项工作。感谢大家的耐心等待。