Can I apply R standard deviation across rows without `apply()` function? [duplicate]

烂漫一生 提交于 2019-12-11 14:55:34

问题


library(tidyverse)
df <- tibble(col1 = c(5, 2), col2 = c(6, 4), col3 = c(9, 9))
# # A tibble: 2 x 3
#    col1  col2  col3
#   <dbl> <dbl> <dbl>
# 1     5     6     9
# 2     2     4     9

df %>% mutate(col4 = apply(.[, c(1, 3)], 1, sum))
df %>% mutate(col4 = rowSums(.[c(1, 3)], na.rm = TRUE))

Lately R's apply() function has been trouble for me. For the time being I'm going to minimize it's use and use alternatives. @akrun educated me that I could use rowSums() instead of apply() as shown above, as an example.

But is there a way to apply, say, standard deviation across columns, like I do below. Obviously my imaginary::rowSd function is not going to work. It's made up.

df %>% mutate(col4 = apply(.[, c(1, 3)], 1, sd))
df %>% mutate(col4 = imaginary::rowSd(.[c(1, 3)], na.rm = TRUE))

What is an approach that would work, without using apply()? I'm thinking purrr although I've little knowledge on this package and the map() functions. Maybe there's an even easier/elegant solution.


[EDIT] I should've mentioned I can't use column names because the names often change in the database I pull my info from. I can only use column numbers, because relative column position doesn't change in the database I pull data from.


回答1:


An easier option is rowSds from matrixStats, but it works only on a matrix, so convert the subset of dataset to matrix and apply rowSds

library(matrixStats)
library(dplyr)
df %>%
    mutate(col4 = rowSds(as.matrix(.[c(1, 3)]))) 
# A tibble: 2 x 4
#   col1  col2  col3  col4
#  <dbl> <dbl> <dbl> <dbl>
#1     5     6     9  2.83
#2     2     4     9  4.95


来源:https://stackoverflow.com/questions/55854415/can-i-apply-r-standard-deviation-across-rows-without-apply-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!