简体   繁体   中英

Convert Character to Numeric

I am not sure the title is worded correctly, I am somewhat new to R. I am using a meaningless task to refresh my memory. I have some United States population data. What I want to do is make a for loop that goes through and plots the population of each state. Here is what I have done so far. Right now I am trying to figure out how to make it print the population as a test.

a <- split(data,list(state)) ##split factor state into individual states

For example str(a$'Alabama'$Population) is numeric of the populations I want to plot.

for(i in 1:53){
b <- noquote((paste(c("a","$", "'",state.list[i],"'", "$","Population"),collapse="")))
}

Now If I print(b) the ouput is a$'Alabama'$Population, however str(print(b)) says it is a character. If I could get it to be a numeric I could move on the next part of my code.

Does this make sense?

Any tips are appreciated. Since this is a learning exercise for me I'd like to know how to solve this problem. However, I am open to better ways of doing this.

dput(head(data, 10))
structure(list(State = structure(c(46L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L), .Label = c("Alabama", "Alaska", "Arizona", "Arkansas", 
"California", "Colorado", "Connecticut", "Delaware", "District of Columbia", 
"Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", 
"Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", 
"Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", 
"Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", 
"New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", 
"Oklahoma", "Oregon", "Pennsylvania", "Puerto Rico", "Rhode Island", 
"South Carolina", "South Dakota", "Tennessee", "Texas", "United States", 
"Utah", "Vermont", "Virginia", "Washington", "West Virginia", 
"Wisconsin", "Wyoming"), class = "factor"), Population = c(308.745538, 
4.779736, 0.710231, 6.392017, 2.915918, 37.253956, 5.029196, 
3.574097, 0.897934, 0.601723), Year = structure(c(6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("2,010", "2,011", "2,012", 
"2,013", "2,014", "Census"), class = "factor")), .Names = c("State", 
"Population", "Year"), row.names = c(NA, 10L), class = "data.frame")

link to full dput : http://pastebin.com/Mg16q67m

link to manual plot http://i.imgur.com/VYvcsqr.jpg

You may need to do the plots individually if you use base graphics, but either lattice or ggplot2 would make much easier work of this. The following gets rid of the Census values (that's not a "year"), cleans up the comma'd year column and uses ggplot to facet plot each state's population growth.

library(ggplot2)

dat <- subset(dat, Year != "Census")
dat$Year <- sub(",", "", dat$Year)

gg <- ggplot(dat)
gg <- gg + geom_line(aes(x=Year, y=Population, group=State))
gg <- gg + geom_point(aes(x=Year, y=Population, group=State))
gg <- gg + scale_x_discrete(breaks=c("2010", "2012", "2014"))
gg <- gg + facet_wrap(~State, scale="free_y", ncol=5)
gg <- gg + labs(x=NULL, y=NULL, title="Population")
gg <- gg + theme_bw()
gg

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM