Question

I am using the package dplyr with R (same problem applies to plyr, too). When I call source("dply_problem.R") to the following code

library("dplyr")

df <- data.frame("A" = 1:6,
                 "B" = 7:12)
mutate(df, C = A + B)

the column "C" does not get added to df. However, when I call mutate(df, C = A + B) from the shell, the column "C" does get created. Could you give me a hint why this is happening?

Was it helpful?

Solution

You haven't assigned the output to anything. Try:

library("dplyr")

df <- data.frame("A" = 1:6,
                 "B" = 7:12)
df <- mutate(df, C = A + B)

df

OTHER TIPS

This can also be done using pipe operator. dplyr imports this operator from another package (magrittr)

library(dplyr)
df <- data.frame("A" = 1:6,
                 "B" = 7:12)
df1<- df %>% mutate(C = A + B)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top