I've found an excellent solution to a problem I'm having where I want to create a new column that computes the mean of all the cells in the corresponding row here:
https://stackoverflow.com/a/33438918/12744116
The data is admittedly not tidy, but the solution, which I've copied below, gets the job done:
data %>%
rowwise() %>%
mutate(c=mean(c(a,b)))
# id a b c
# (dbl) (dbl) (dbl) (dbl)
# 1 101 1 2 1.5
# 2 102 2 2 2.0
# 3 103 3 2 2.5
However, unlike this simpler example, I have far too many columns to name. I'm wondering if there's any way of quickly referring to the columns using slicing notation (ie, instead of c(a, b), something like 2:3) or some other way of referring to the columns via their index.
I've found something similar on another Stack Overflow thread here , but the solution has its own problems since we're listing all the column indices instead of the column names. I have way too many columns for me to list them all for each calculation.
Any solutions?
EDIT: I figured one out myself, but I feel like it's too inelegant and I believe I'm maybe extracting the entire column for every row, which is obviously going to be a slower solution than expected:
data %>%
mutate(id = row_number()) %>%
rowwise() %>%
mutate(avg = mean(c(.[id, 2:4], recursive=TRUE)))
Any solutions that are faster?
You can do:
df %>%
mutate(c = rowMeans(select(., 2:3)))
id a b c
1 101 1 2 1.5
2 102 2 2 2.0
3 103 3 2 2.5
Or:
df %>%
mutate(c = rowMeans(select(., 2:length(.))))
For me using rowMeans
seems straightforward without involving tidyverse
functions.
data$c <- rowMeans(data[2:3])
however, if you prefer tidyverse
solution we can take a bit of help from purrr
map
functions.
library(dplyr)
library(purrr)
For only two columns
data %>% mutate(c = map2_dbl(a, b, ~mean(c(.x, .y))))
For many columns
data %>% mutate(c = pmap_dbl(select(., a:b), ~mean(c(...))))
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.