简体   繁体   中英

Mutating column in `dplyr` using `rowSums`

Recently I stumbled uppon a strange behaviour of dplyr and I would be happy if somebody would provide some insights.

Assuming I have a data of which com columns contain some numerical values. In an easy scenario I would like to compute rowSums . Although there are many ways to do it, here are two examples:

df <- data.frame(matrix(rnorm(20), 10, 2),
                 ids = paste("i", 1:20, sep = ""),
                 stringsAsFactors = FALSE)

# works
dplyr::select(df, - ids) %>% {rowSums(.)}

# does not work
# Error: invalid argument to unary operator
df %>%
  dplyr::mutate(blubb = dplyr::select(df, - ids) %>% {rowSums(.)})

# does not work
# Error: invalid argument to unary operator
df %>%
  dplyr::mutate(blubb = dplyr::select(., - ids) %>% {rowSums(.)})

# workaround:
tmp <- dplyr::select(df, - ids) %>% {rowSums(.)}
df %>%
  dplyr::mutate(blubb = tmp)

# works
rowSums(dplyr::select(df, - ids))

# does not work
# Error: invalid argument to unary operator
df %>%
  dplyr::mutate(blubb = rowSums(dplyr::select(df, - ids)))

# workaround
tmp <- rowSums(dplyr::select(df, - ids))
df %>%
  dplyr::mutate(blubb = tmp)

First, I don't really understand what is causing the error and second I would like to know how to actually achieve a tidy computation of some (viable) columns in a tidy way.

edit

The question mutate and rowSums exclude columns , although related, focuses on using rowSums for computation. Here I'm eager to understand why the upper examples do not work. It is not so much about how to solve (see the workarounds) but to understand what happens when the naive approach is applied.

The examples do not work because you are nesting select in mutate and using bare variable names. In this case, select is trying to do something like

> -df$ids
Error in -df$ids : invalid argument to unary operator

which fails because you can't negate a character string (ie -"i1" or -"i2" makes no sense). Either of the formulations below works:

df %>% mutate(blubb = rowSums(select_(., "X1", "X2")))
df %>% mutate(blubb = rowSums(select(., -3)))

or

df %>% mutate(blubb = rowSums(select_(., "-ids")))

as suggested by @Haboryme.

select_ is deprecated . You can use:

library(dplyr)
df <- data.frame(matrix(rnorm(20), 10, 2),
                 ids = paste("i", 1:20, sep = ""),
                 stringsAsFactors = FALSE)
df %>% 
  mutate(blubb = rowSums(select(., .dots = c("X1", "X2"))))

# Or more generally:
desired_columns <- c("X1", "X2")
df %>% 
  mutate(blubb = rowSums(select(., .dots = all_of(desired_columns))))

select can now accept bare column names so no need to use .dots or select_ which has been deprecated.

Here are few of the approaches that can work now.

library(dplyr)

#sum all the columns except `id`. 
df %>% mutate(blubb = rowSums(select(., -ids), na.rm = TRUE))

#sum X1 and X2 columns
df %>% mutate(blubb = rowSums(select(., X1, X2), na.rm = TRUE))

#sum all the columns that start with 'X'
df %>% mutate(blubb = rowSums(select(., starts_with('X')), na.rm = TRUE))

#sum all the numeric columns
df %>% mutate(blubb = rowSums(select(., where(is.numeric))))

Adding to this old thread because I searched on this question then realized I was asking the wrong question. Also, I detect some yearning in this and related questions for the proper pipe steps way to do this.

The answers here are somewhat non-intuitive because they are trying to use the dplyr vernacular with non-"tidy" data. IF you want to do it the dplyr way, make the data tidy first, using gather() , and then use summarise()

library(tidyverse)

df <- data.frame(matrix(rnorm(20), 10, 2),
                 ids = paste("i", 1:20, sep = ""),
                 stringsAsFactors = FALSE)

df %>% gather(key=Xn,value="value",-ids) %>% 
  group_by(ids) %>% 
  summarise(rowsum=sum(value))

#> # A tibble: 20 x 2
#>    ids   rowsum
#>    <chr>       <dbl>
#>  1 i1          0.942
#>  2 i10        -0.330
#>  3 i11         0.942
#>  4 i12        -0.721
#>  5 i13         2.50 
#>  6 i14        -0.611
#>  7 i15        -0.799
#>  8 i16         1.84 
#>  9 i17        -0.629
#> 10 i18        -1.39 
#> 11 i19         1.44 
#> 12 i2         -0.721
#> 13 i20        -0.330
#> 14 i3          2.50 
#> 15 i4         -0.611
#> 16 i5         -0.799
#> 17 i6          1.84 
#> 18 i7         -0.629
#> 19 i8         -1.39 
#> 20 i9          1.44

If you care about the order of the ids when they are not sortable using arrange() , make that column a factor first.

  df %>% 
  mutate(ids=as_factor(ids)) %>% 
  gather(key=Xn,value="value",-ids) %>% 
  group_by(ids) %>% 
  summarise(rowsum=sum(value))

Why do you want to use the pipe operator? Just write an expression such as:

rowSums(df[,sapply(df, is.numeric)])

ie calculate the rowsums on all the numeric columns, with the advantage of not needing to specify ids .

If you want to save your results as a column within data, you can use data.table syntax like this:

dt <- as.data.table(df)
dt[, x3 := rowSums(.SD, na.rm=T), .SDcols = which(sapply(dt, is.numeric))]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM