简体   繁体   中英

Using the na.rm = TRUE option in the function for summary stats - summary()

I am trying to calculate the summary stats of a few columns in a data frame. Some of the cells have NA values and when using the summary() function, I want R to exclude the values that have NA. I can't delete the rows because not all values in a row are NAs. When I use the mean() or sd() function individually with na.rm = TRUE, that works. But when I use the summary() function, na.rm = TRUE makes no difference.

Either of These work:

mean(df1[9:24,9], na.rm = TRUE) # OR
sd(df1[9:24,9], na.rm = TRUE)

but summary does not :

summary(df1[9:24,9], na.rm = TRUE)

The summary() function returns the same answer whether na.rm = TRUE is inserted or not. Is there some way I can use the summary() function and also make it provide summary stats of the data subset ensuring that the NAs value do not have to be counted ?

You can try out the following, with the help of subset.

mean(subset(df[9:24,9],!is.na(df1[9:24,9])))
sd(subset(df[9:24,9],!is.na(df1[9:24,9])))
summary(subset(df[9:24,9],!is.na(df1[9:24,9])))

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