总结()警告:参数不是数字或逻辑:返回 NA

summarize() warning: argument is not numeric or logical: returning NA

我正在尝试使用 dplyr 在我的数据中找到按年份计算的平均值。我不明白为什么代码会给我 NA。这是我的代码:

S1b$Loans.and.discounts <- gsub(",","",S1b$Loans.and.discounts)
S1b$Loans.and.discounts <- as.numeric(S1b$Loans.and.discounts)
S1b <- S1b %>% group_by(Year) %>% summarize(loans_discounts=mean(county_panel$Loans.and.discounts, na.rm=T))

它给了我以下警告信息:

"在 mean.default(county_panel$Loans.and.discounts, na.rm = T) 中: 参数不是数字或逻辑:返回 NA"

然而 is.numeric(S1b$Loans.and.discounts) 返回 TRUE。我确定我犯了一个简单的错误,但我想不通。

感谢您的帮助!

这是我的前 6 行数据:

        S1b <- structure(list(yearid = c("BARBOURAlabamaEastern Bank of AlabamaEufaula AL United States1859", 
"DALLASAlabamaBank of SelmaSelma AL United States1860", "DALLASAlabamaBank of SelmaSelma AL United States1861", 
"DALLASAlabamaCommercial Bank of AlabamaSelma AL United States1858", 
"DALLASAlabamaCommercial Bank of AlabamaSelma AL United States1858", 
"DALLASAlabamaCommercial Bank of AlabamaSelma AL United States1859"
), Loans.and.discounts = structure(c(2L, 76L, 79L, 105L, 126L, 
10L), .Label = c("1,036,732.32", "1,046,344.00", "1,059,448.00", 
"1,064,412.02", "1,123,057.00", "1,134,057.08", "1,203,749.19", 
"1,261,178.00", "1,267,007.00", "1,294,503.00", "1,371,251.00", 
"1,382,316.00", "1,428,948.03", "1,430,751.00", "1,435,369.00", 
"1,442,768.40", "1,478,016.00", "1,500,152.00", "1,566,613.00", 
"1,586,888.00", "1,686,925.00", "1,688,333.02", "1,710,964.00", 
"1,735,238.22", "1,736,139.00", "1,744,934.00", "1,799,224.00", 
"1,828,353.31", "1,833,584.00", "1,855,272.00", "1,877,776.00", 
"1,890,808.80", "1,893,964.00", "1,909,825.20", "1,922,956.00", 
"1,952,375.00", "1,956,057.00", "1,994,781.03", "1,996,300.00", 
"139,215.00", "2,025,011.00", "2,065,108.45", "2,082,529.00", 
"2,127,799.00", "2,141,789.00", "2,142,311.00", "2,186,245.00", 
"2,205,359.00", "2,207,533.10", "2,224,840.80", "2,240,610.00", 
"2,273,343.69", "2,273,427.69", "2,303,140.00", "2,345,513.31", 
"2,370,536.96", "2,374,253.31", "2,380,343.00", "2,385,165.00", 
"2,511,346.00", "2,517,237.85", "2,567,402.00", "2,570,795.00", 
"2,603,449.00", "2,681,028.00", "2,733,045.89", "2,806,416.00", 
"2,818,021.93", "2,845,855.00", "2,936,997.00", "2,943,118.89", 
"2,951,269.00", "2,967,790.77", "2,971,323.00", "201,897.00", 
"208,991.00", "255,312.00", "256,858.00", "295,836.00", "3,009,071.95", 
"3,015,990.00", "3,083,618.00", "3,102,461.35", "3,135,833.35", 
"3,169,470.00", "3,265,545.00", "3,273,119.00", "3,280,694.00", 
"3,349,759.00", "3,407,438.86", "3,433,263.04", "3,596,460.00", 
"3,720,258.00", "3,748,473.00", "3,781,071.00", "3,999,250.00", 
"312,625.00", "329,828.00", "330,726.00", "337,915.00", "359,461.00", 
"371,394.00", "398,448.00", "4,147,912.19", "446,867.00", "5,141,469.23", 
"5,284,763.00", "5,409,653.65", "5,953,621.00", "554,726.00", 
"558,114.00", "6,026,826.00", "6,285,744.15", "6,603,403.00", 
"6,681,923.12", "600,810.00", "602,110.00", "605,939.00", "7,116,768.83", 
"7,275,751.36", "72,117.60", "783,223.00", "8,088,453.00", "8,175,581.00", 
"883,968.03", "988,263.00", "99,324.36"), class = "factor"), 
    Year = structure(c(30L, 31L, 32L, 29L, 29L, 30L), .Label = c("1830", 
    "1831", "1832", "1833", "1834", "1835", "1836", "1837", "1838", 
    "1839", "1840", "1841", "1842", "1843", "1844", "1845", "1846", 
    "1847", "1848", "1849", "1850", "1851", "1852", "1853", "1854", 
    "1855", "1856", "1857", "1858", "1859", "1860", "1861"), class = "factor")), row.names = c(1128L, 
6505L, 6517L, 6865L, 6876L, 6888L), class = "data.frame")

改变

S1b <- S1b %>% group_by(Year) %>% 
summarize(loans_discounts=mean(county_panel$Loans.and.discounts, na.rm=T))

S1b <- S1b %>% group_by(Year) %>% 
summarize(loans_discounts=mean(Loans.and.discounts, na.rm=TRUE))