means and SD for columns in a dataframe with NA values

前端 未结 3 1603
庸人自扰
庸人自扰 2021-01-20 01:58

I\'m trying to calculate the mean and standard deviation of several columns (except the first column) in a data.frame with NA values.

I\'ve tried

3条回答
  •  醉话见心
    2021-01-20 02:25

    The following example code may prove useful.

    # Create a 5 column dataframe that contains some NAs
    col1 <- c(1,2,3,4,5)
    col2 <- c(6,7,8,9,10)
    col3 <- c(11,12,13,14,NA)
    col4 <- c(16,NA,18,19,20)
    col5 <- c(21,22,23,24,NA)
    dataframe <- data.frame(col1,col2,col3,col4,col5)
    
    # Apply the mean() function to all but the first column of the dataframe
    apply(dataframe[,2:ncol(dataframe)], 2, function(x) mean(x, na.rm=TRUE))
    
    # Check that the returned values are correct:
    mean(col2)
    mean(col3, na.rm=TRUE)
    mean(col4, na.rm=TRUE)
    mean(col5, na.rm=TRUE)
    

    For the standard deviation, replace mean() with sd().

提交回复
热议问题