Nicholas Root
Nicholas Root

Reputation: 555

error with group_by, mutate, and scale together

test = data.frame(a=factor(c("A","A","B","B","C","C")),
                  b=c(101,103,50,100,50,60),
                  c=factor(c("John","Mary","John","Mary","John","Mary"))) %>%
  group_by(c) %>% mutate(d=scale(b))
View(test)

Why does this produce the error Error in FUN(X[[i]], ...) : dims [product 3] do not match the length of object [6]

I'm trying to z-score values of b within each level of c (i.e. "per subject").

Upvotes: 1

Views: 901

Answers (2)

tblznbits
tblznbits

Reputation: 6778

The following works:

test = data.frame(a=factor(c("A","A","B","B","C","C")),
               b=c(101,103,50,100,50,60), 
             c=factor(c("John","Mary","John","Mary","John","Mary"))) %>% 
   group_by(c) %>% mutate(d=scale(b)[, 1])
View(test)

This is because scales returns a matrix as it's value, and the above takes the first column of that matrix.

Upvotes: 1

tospig
tospig

Reputation: 8333

You are getting attributes inside column d, and R doesn't know how to view these. Wrapping the scale in as.numeric() removes these attributes.

library(dplyr)
test = data.frame(a=factor(c("A","A","B","B","C","C")),
                                    b=c(101,103,50,100,50,60),
                                    c=factor(c("John","Mary","John","Mary","John","Mary"))) %>%
    group_by(c) %>% 
    mutate(d = as.numeric(scale(b)))

View(test)

Upvotes: 4

Related Questions