Say I have the following data:
colA <- c(\"SampA\", \"SampB\", \"SampC\")
colB <- c(21, 20, 30)
colC <- c(15, 14, 12)
colD <- c(10, 22, 18)
df &l
A different tidyverse
approach could be:
df %>%
rowid_to_column() %>%
gather(var, val, -c(colA, rowid)) %>%
group_by(rowid) %>%
summarise(rsds = sd(val)) %>%
left_join(df %>%
rowid_to_column(), by = c("rowid" = "rowid")) %>%
select(-rowid)
rsds colA colB colC colD
1 5.51 SampA 21 15 10
2 4.16 SampB 20 14 22
3 9.17 SampC 30 12 18
Here it, first, creates a row ID. Second, it performs a wide-to-long data transformation, excluding the "colA" and row ID. Third, it groups by row ID and calculates the standard deviation. Finally, it joins it with the original df on row ID.
Or alternatively, using rowwise()
and do()
:
df %>%
rowwise() %>%
do(data.frame(., rsds = sd(unlist(.[2:length(.)]))))
colA colB colC colD rsds
*
1 SampA 21 15 10 5.51
2 SampB 20 14 22 4.16
3 SampC 30 12 18 9.17