Calculating standard deviation of each row

前端 未结 2 588
心在旅途
心在旅途 2020-12-06 06:28

I am trying to use rowSds()to calculate each rows standard deviation so that I can pick the rows that have high sds to graph.

My data frame is called

2条回答
  •  独厮守ぢ
    2020-12-06 07:11

    Also works, based on this answer

    set.seed(007)
    X <- data.frame(matrix(sample(c(10:20, NA), 100, replace=TRUE), ncol=10))
    
    vars_to_sum = grep("X", names(X), value=T)
    X %>% 
      group_by(row_number()) %>%
      do(data.frame(., 
                    SD = sd(unlist(.[vars_to_sum]), na.rm=T)))
    

    ...which appends a couple of row number columns, so probably better to explicitly add your row IDs for grouping.

    X %>% 
      mutate(ID = row_number()) %>%
      group_by(ID) %>%
      do(data.frame(., SD = sd(unlist(.[vars_to_sum]), na.rm=T)))
    

    This syntax also has the feature of being able to specify which columns you want to use.

提交回复
热议问题