mutate() to create new variables Q

So, I have imported a data set as a tibble, using read_csv. When I try to add a variable with mutate, it seems to get added, but the name of the variable is not stored with the name I gave it, instead it is stored as the arithmetic that I did to come up with the new variable with the existing variables. The variable name is one variable / by other variable).

Why is it not giving it the name I gave it? As a result of this, I can't find the variable when I try to select it with select()

Cheers for any help

The behavior you describe suggests that you are not providing the name for the new column properly. Below is an example of mutate() using the calculation as the column name and an example of providing the column name correctly. If that does not clear up your problem, please post your actual code.

library(dplyr)
df <- data.frame(A = 1:4, B = 11:14)
df
#>   A  B
#> 1 1 11
#> 2 2 12
#> 3 3 13
#> 4 4 14
df <- df %>% mutate(A/B)
df
#>   A  B        A/B
#> 1 1 11 0.09090909
#> 2 2 12 0.16666667
#> 3 3 13 0.23076923
#> 4 4 14 0.28571429
df <- df %>% mutate(NewVar = A/B)
df
#>   A  B        A/B     NewVar
#> 1 1 11 0.09090909 0.09090909
#> 2 2 12 0.16666667 0.16666667
#> 3 3 13 0.23076923 0.23076923
#> 4 4 14 0.28571429 0.28571429

Created on 2019-08-20 by the reprex package (v0.2.1)

2 Likes

Thanks. How would you create the new variable without the pipe operators?

df <- mutate(df, NewVar = A/B)

The pipe operator is merely a shortcut that lets you concatenate functions, but you can just add the data source (df) as parameter to the call (mutate).

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.