I imported some data from a txt file using then isolated the columns that are relevant (6 and 11) and renamed from V6 to state and from V11 to population:
customerDataOE <- read.table(file.choose(),
sep = "\t",
skip = 2,
fill = T)
customerStateAndPopOE <- customerDataOE[,c(6,11)]
colnames(customerStateAndPopOE) <- c("state", "population")
A shortened version of my data looks like this in the environment in RStudio
state population
[1] Wisconsin: Milwaukee County 0
[2] Delaware: 59,859
[3] Washington: King County, Whatcom County 56000
[4] Washington: Kitsap County, Skagit County 25
[5] Washington: Island County Unknown
[6] Washington: Snohomish County 1,400
This is in a data.frame class and I want to loop through the state to find if it contains the phrase Washington. If it does, I want to go to population on that same row and add the populations up via a cumulative sum. My end result should be like this: Washington 57425 in a new data.frame
I have tried using grepl but it does not work:
for(i in 1:nrow(customerStateAndPopOE)) {
if(grepl('Washington', customerStateAndPopOE$state, ignore.case = TRUE))
Aucun commentaire:
Enregistrer un commentaire