Why is mutate function not adding a new column to my table

dplyr functions do not perform in-place modifications, they return a new data frame as output, if you want changes to persist, you have to explicitly overwrite the original data frame with the assign operator. This is an example of the syntax with pseudo code.

original_dataframe <- original dataframe %>%
    mutate(new_column = column_1 - column_2)

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

4 Likes

Beautiful! Incredible!
This worked perfectly thank you

When we add select to select columns, the code works;
penguins %>% select (species, island, body_mass_g) %>% mutate (body_mass_kg = body_mass_g/1000)

1 Like

The mutate() operation works the same even if you do not select() those variables first. This doesn't address the problem described by @Advance.

They were asking why that specific code was not adding a new column to the original penguins data set, which you can see here it doesn't, because dplyr doesn't perform in-place modifications.

library(dplyr)
library(palmerpenguins)

penguins %>%
    mutate (body_mass_kg = body_mass_g / 1000)
#> # A tibble: 344 × 9
#>    species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
#>    <fct>   <fct>              <dbl>         <dbl>             <int>       <int>
#>  1 Adelie  Torgersen           39.1          18.7               181        3750
#>  2 Adelie  Torgersen           39.5          17.4               186        3800
#>  3 Adelie  Torgersen           40.3          18                 195        3250
#>  4 Adelie  Torgersen           NA            NA                  NA          NA
#>  5 Adelie  Torgersen           36.7          19.3               193        3450
#>  6 Adelie  Torgersen           39.3          20.6               190        3650
#>  7 Adelie  Torgersen           38.9          17.8               181        3625
#>  8 Adelie  Torgersen           39.2          19.6               195        4675
#>  9 Adelie  Torgersen           34.1          18.1               193        3475
#> 10 Adelie  Torgersen           42            20.2               190        4250
#> # … with 334 more rows, and 3 more variables: sex <fct>, year <int>,
#> #   body_mass_kg <dbl>

# The column doesn't exist because the output hasn't been explicitly assigned
# to the original data frame
penguins$body_mass_kg
#> Warning: Unknown or uninitialised column: `body_mass_kg`.
#> NULL

You need to explicitly assign the output to the original data set if you want the changes to persist.

library(dplyr)
library(palmerpenguins)

penguins <- penguins %>%
    mutate (body_mass_kg = body_mass_g / 1000)

penguins$body_mass_kg
#>   [1] 3.750 3.800 3.250    NA 3.450 3.650 3.625 4.675 3.475 4.250 3.300 3.700
#>  [13] 3.200 3.800 4.400 3.700 3.450 4.500 3.325 4.200 3.400 3.600 3.800 3.950
#>  [25] 3.800 3.800 3.550 3.200 3.150 3.950 3.250 3.900 3.300 3.900 3.325 4.150
#>  [37] 3.950 3.550 3.300 4.650 3.150 3.900 3.100 4.400 3.000 4.600 3.425 2.975
#>  [49] 3.450 4.150 3.500 4.300 3.450 4.050 2.900 3.700 3.550 3.800 2.850 3.750
#>  [61] 3.150 4.400 3.600 4.050 2.850 3.950 3.350 4.100 3.050 4.450 3.600 3.900
#>  [73] 3.550 4.150 3.700 4.250 3.700 3.900 3.550 4.000 3.200 4.700 3.800 4.200
#>  [85] 3.350 3.550 3.800 3.500 3.950 3.600 3.550 4.300 3.400 4.450 3.300 4.300
#>  [97] 3.700 4.350 2.900 4.100 3.725 4.725 3.075 4.250 2.925 3.550 3.750 3.900
#> [109] 3.175 4.775 3.825 4.600 3.200 4.275 3.900 4.075 2.900 3.775 3.350 3.325
#> [121] 3.150 3.500 3.450 3.875 3.050 4.000 3.275 4.300 3.050 4.000 3.325 3.500
#> [133] 3.500 4.475 3.425 3.900 3.175 3.975 3.400 4.250 3.400 3.475 3.050 3.725
#> [145] 3.000 3.650 4.250 3.475 3.450 3.750 3.700 4.000 4.500 5.700 4.450 5.700
#> [157] 5.400 4.550 4.800 5.200 4.400 5.150 4.650 5.550 4.650 5.850 4.200 5.850
#> [169] 4.150 6.300 4.800 5.350 5.700 5.000 4.400 5.050 5.000 5.100 4.100 5.650
#> [181] 4.600 5.550 5.250 4.700 5.050 6.050 5.150 5.400 4.950 5.250 4.350 5.350
#> [193] 3.950 5.700 4.300 4.750 5.550 4.900 4.200 5.400 5.100 5.300 4.850 5.300
#> [205] 4.400 5.000 4.900 5.050 4.300 5.000 4.450 5.550 4.200 5.300 4.400 5.650
#> [217] 4.700 5.700 4.650 5.800 4.700 5.550 4.750 5.000 5.100 5.200 4.700 5.800
#> [229] 4.600 6.000 4.750 5.950 4.625 5.450 4.725 5.350 4.750 5.600 4.600 5.300
#> [241] 4.875 5.550 4.950 5.400 4.750 5.650 4.850 5.200 4.925 4.875 4.625 5.250
#> [253] 4.850 5.600 4.975 5.500 4.725 5.500 4.700 5.500 4.575 5.500 5.000 5.950
#> [265] 4.650 5.500 4.375 5.850 4.875 6.000 4.925    NA 4.850 5.750 5.200 5.400
#> [277] 3.500 3.900 3.650 3.525 3.725 3.950 3.250 3.750 4.150 3.700 3.800 3.775
#> [289] 3.700 4.050 3.575 4.050 3.300 3.700 3.450 4.400 3.600 3.400 2.900 3.800
#> [301] 3.300 4.150 3.400 3.800 3.700 4.550 3.200 4.300 3.350 4.100 3.600 3.900
#> [313] 3.850 4.800 2.700 4.500 3.950 3.650 3.550 3.500 3.675 4.450 3.400 4.300
#> [325] 3.250 3.675 3.325 3.950 3.600 4.050 3.350 3.450 3.250 4.050 3.800 3.525
#> [337] 3.950 3.650 3.650 4.000 3.400 3.775 4.100 3.775

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.