Error in `[.data.frame`(county.data, 1, Col.2008) : undefined columns selected.

I am using a code I was given that will create a looped series of multiple pie charts but I keep receiving an error code. The code is

Error in [.data.frame(county.data, 1, Col.2008) :
undefined columns selected.

Can you please help. code is as follows

Counties <- c ("King County", "Pierce County", "Snohomish County")
Col.2008 <- c("gop_2008","dem_2008","other_2008")
Col.2012 <- c("gop_2012","dem_2012","other_2012")
Col.2016 <- c("gop_2016","dem_2016","other_2016")
labels <- c("GOP","Dem","Other")
colors <- c("Red","Blue","Green")
for (county in Counties)

{
county.data <- MyElectionData[MyElectionData$county==county,]
pie(as.numeric(county.data[1,Col.2008]),
labels = labels, col = colors, main = paste(county,"2008"))
pie(as.numeric(county.data[1,Col.2012]),
labels = labels, col = colors, main = paste(county,"2012"))
pies(as.numeric(county.data[1,Col.2016]),
labels=labels, col=colors, main=paste(county,"2016"))
}

Error in MyElectionData : object 'MyElectionData' not found

what is this object?

1 Like

What code are you using to bring the Excel document into R?

It is a csv file under excel and I am opening it under the Import Dataset by text based

j2008,2012,2016 and all the counties in Washington State

it also has a "State" section but it is not visible in the picture

Pictures are not so helpful as text outputs. Did you specifically choose not to run the code I suggested to you?
Your screenshot doesn't show a column named Col.2008 so this is likely the first point of failure of your code

I figured it out. The issue was in other_2008 rstudio wants oth_2008

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

It is an excel document that we are using in class to create pie charts of the county voting percentages.

What variables are listed as being present in it?

names(MyElectionData)