简体   繁体   中英

R Function to calculate percentage of vector below the mean

I have pulled in a csv file from a url and cleaned it up. I am now trying to create a function that takes two inputs, a vector and a number, and tells you the percentage of values in the vector that fall below the number. The vector will be dfStates$jul2011, and the number will be the mean of dfStates$jul2011. Can anyone point me in the right direction to creating this function?

readStates <- read.csv(url("http://www2.census.gov/programs-
surveys/popest/tables/2010-2011/state/totals/nst-est2011-01.csv"))  #Assign 
readStates

readStates[6:10] <- list(NULL) #Remove Variables x.4-x.8

readStates <- readStates[-c(1, 2, 3, 4, 5, 6, 7, 8, 60, 61, 62, 63, 64, 65, 
66, 67),] #Remove Rows

colnames(readStates)[1] <- "stateName"                                                       
#Fix Column Name  
colnames(readStates)[2] <- "base2010"                                                        
#Fix Column Name
colnames(readStates)[3] <- "base2011"                                                        
#Fix Column Name
colnames(readStates)[4] <- "jul2010"                                                         
#Fix Column Name
colnames(readStates)[5] <- "jul2011"                                                         
#Fix Column Name

dfStates <- 
as.data.frame(sapply(readStates,gsub,pattern="\\.",replacement=""))              
#Remove Periods

dfStates <- as.data.frame(sapply(dfStates,gsub,pattern=",",replacement=""))                  
#Remove Commas  

dfStates$base2010 <- as.numeric(as.character(dfStates$base2010))                             
#Set as Numeric
dfStates$base2011 <- as.numeric(as.character(dfStates$base2011))                             
#Set as Numeric
dfStates$jul2010 <- as.numeric(as.character(dfStates$jul2010))                               
#Set as Numeric
dfStates$jul2011 <- as.numeric(as.character(dfStates$jul2011))                               
#Set as Numeric

HI you can take mean of your vector

me<-mean(dfStates$jul2011)

Then put this into a different vector and calculate the values less than mean and count number of values. Then simply calculate the percentage.

temp_vec<-dfStates$jul2011
len<-length(temp_vec)
temp_vec2<-temp_vec[temp_vec<me]
len_temp_vec2<-length(temp_vec2)
per<-(len_temp_vec2/len)*100
per

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