I'm creating a custom function, to take a closer look into data frames, that have election results in them. Columns consist of political parties and he rows consist of electoral district. My custom function takes two arguments: The data frame (df) and the district (region). The function is supposed to return three biggest parties in the given district, and the percentage of vote each party got.
I've run into a problem with the function. While my function works wonderfully when given a region that does exist within the data frame, it does not however print the else command given within the function when region does not exist. Instead of returning "Region does not exist", it instead gives me Error in [<-.data.frame
(*tmp*
, , 1, value = NA) :
replacement has 1 row, data has 0
This is an error I'm not familiar with, and googling it only seems to indicate to me that the error is unique to the case. Thus, what am I missing in my function? I guess I've become blind to the function since I've been looking at it for a while. How can I fix this error?
top3 <- function(df, region){
rownames <- df[,1]
df <- as.data.frame(df, row.names = rownames)
aapo <- subset(df, Alue == region)
aapo[,1] <- NA
aapo[,22] <- NA
TT <- sort(aapo, decreasing = TRUE)
tulos <- head(TT)[1:3]
if (alue %in% rownames) {
result <- tulos
} else {
result <- "Region does not exist"
}
return(result)
}