我正在嘗試向我的數據框添加一個名爲Solvent的列,並將根據溶劑利用率返回yes或no,我已成功添加另一列獲取縣的數據,但我的溶劑代碼不起作用。我收到錯誤:「錯誤的SCC $ SCC.Level.One:$操作是原子向量無效」R基於原子矢量數據向列數據框添加列
NEI <- readRDS("summarySCC_PM25.rds")
SCC <- readRDS("Source_Classification_Code.rds")
library(ggplot2)
form <- NEI[,c("fips","SCC", "Emissions")]
LANEI <- form[form$fips=="06037",]
OCNEI <- form[form$fips == "06059",]
SBCNEI <- form[form$fips =="06071",]
AllNEI<-rbind(rbind(LANEI,OCNEI), SBCNEI)
GetCounty <-function (fips)
{
if (fips == "06037")
return ("Los Angeles")
else if (fips == "06059")
return ("Orange County")
if (fips == "06071")
return ("San Bernardino County")
}
chckSolv <- function (SCC)
{
if (SCC$SCC.Level.One == "Solvent Utilization")
return ("Yes")
else
return ("No")
}
CountyData <- sapply(AllNEI$fips, GetCounty)
solventData <- sapply(AllNEI$SCC,chckSolv)
AllNEI <-cbind (AllNEI, Solvent = solventData)
AllNEI <- cbind(AllNEI, county = CountyData)
什麼是' 「Source_Classification_Code.rds」'結構? –
我認爲問題在於你傳遞了一個單獨的向量到你的'chckSolv'函數,但是它看起來應該帶一個名爲'SCC.Level.One'的列。嘗試'solventData < - sapply(AllNEI,chckSolv)' –
僅供參考,您可以考慮使用向量化函數,如'ifelse()'而不是'chckSolv'和'GetCounty'函數。它會更快,並且會實現相同的目標。 –