Use the R package “UsingR” to download the data file (called “aid”) about monthl
ID: 2949080 • Letter: U
Question
Use the R package “UsingR” to download the data file (called “aid”) about monthly payment for federal program to R.
Use R code to check whether it is a vector or matrix. Then assign “payment” as a vector based on the payment amounts only by removing the names of the states.
Use the appropriate vector functions to rank, to sort, and to order the payment vector. Call these vectors as rank.payment, sorted.payment, and ordered.payment, Then use R code to make a dataframe (or table) out of these four vectors.
Use R code to save the dataframe or table in the text file called “table.txt” in your working directory.
Use R code to find the name of the states with highest and lowest payment.
Explanation / Answer
library(UsingR)
data(aid) #read data
#check vector or matrix
if(is.vector(aid)){
print("Vector")
} else if(is.matrix(aid)){
print("Matrix")
}
#remove names
payment <- unname(aid)
#rank, order, sort
rank.payment <- rank(payment)
ordered.payment<- payment[order(payment)]
sorted.payment<-sort(payment)
#create dataframe
tb <-data.frame(col1 = payment, col2= rank.payment, col3= sorted.payment, col4 = ordered.payment)
#write dataframe
write.table(tb,"table.txt",sep=" ",row.names=FALSE)
#lowest payment from rank ordering
attributes(aid)$names[match(1, rank.payment)]
#highest payment from rank ordering
attributes(aid)$names[match(51, rank.payment)]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.