R Stete.x77 Exercises


#system.file("data",package = "datasets")

#conver dataset to data frame
st<-data.frame(state.x77)
class(st)
attach(st)
st

#get higest murder state
mx<-st[,"Murder"]
st[st$Murder==max(c(mx)) ,]

#get higest and lowest murder
mx<-st[,"Murder"]
mn<-st[,"Murder"]
st[st$Murder==max(c(mx)) | st$Murder==min(c(mn))  ,]

#top 5 popular state
pop<-st[order(Population,decreasing=T),]
head(pop,n=5)

#bottom 3 life expe
lyf<-st[order(Life.Exp,decreasing=T),]
tail(lyf,n=3)

#display greater than avg income
av<-st[,"Income"]
av<-sum(av)/length(av)
st[Income>av ,]

#display greater than avg income
av<-st[,"Income"]
av<-sum(st[,"Income"])/length(av)
st[Income < av, ]

Post a Comment

Previous Post Next Post