MattCom MattCom - 1 year ago 56
R Question

R Adding a column to a data frame based on atomic vector data

I'm trying to add a column to my data frame that will be named Solvent and will either return yes or no based on solvent utilization, I've successfully added one other column that gets the county data but my code for solvent isn't working. I receive the error: "Error in SCC$SCC.Level.One : $ operator is invalid for atomic vectors"

NEI <- readRDS("summarySCC_PM25.rds")
SCC <- readRDS("Source_Classification_Code.rds")


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")
return ("No")

CountyData <- sapply(AllNEI$fips, GetCounty)
solventData <- sapply(AllNEI$SCC,chckSolv)
AllNEI <-cbind (AllNEI, Solvent = solventData)
AllNEI <- cbind(AllNEI, county = CountyData)

Answer Source

Consider using the vectorized ifelse(). Your earlier error is due to referencing a named element, SCC.Level.On on a specific value, SCC.

AllNEI$CountyData <- ifelse(AllNEI$fips == "06037", "Los Angeles",
                            ifelse(AllNEI$fips == "06059", "Orange County", 
                                   ifelse(AllNEIfips == "06071", "San Bernardino County", NA)

AllNEI$SolventData <- ifelse(AllNEI$SCC == "Solvent Utilization", "Yes", "No")

Additionally, you can reduce rbind operations:

AllNEI <- NEI[NEI$fips %in% c("06037", "06059", "06071"), c("fips", "SCC", "Emissions")]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download