Home > OS >  issue with datasets,mutate in r
issue with datasets,mutate in r

Time:12-17

enter image description hereenter image description hereI mutate a new column and I write view(), then shows new column. However, that's after calling view(dataset) and does not show that new column or colnames(). It does not show a new variable column name.

How to join a new computed column in a datasets permanently in R?

In the first chunk of code, I select year, name, and number (baby borns in yaear) - 3 variable actual data set. I also then compute the column year_total. When I run the code, the number cannot be found. How are all individual variable saved in the environment from a datasets in r?

babynames |>
  select(year,name,number) |>
  group_by(year) |>
mutate(year_total = sum(number) ) |>
  View()

babynames |>
  select(year,name,number,year_total) |>
  mutate(fraction_people = number / year_total) |>
  View()

dput(babynames)

CodePudding user response:

I think it's just a typo. There is no variable called number in the babynames data, assuming you're using the one from the babynames package. There is a variable called n. The following works for me:

library(babynames)
data(babynames)
babynames |>
  rename(number = n) |>
  select(year,name,number) |>
  group_by(year) |>
  mutate(year_total = sum(number) ) |>
  View()

CodePudding user response:

You haven't reassigned the dataset into a new object.

Doing this will return a dataset with your new column created, but it won't change the dataset itself:

babynames |>
  rename(number = n) |>
  select(year,name,number) |>
  group_by(year) |>
  mutate(year_total = sum(number) )

To replace the dataframe with the new version, you need to do this:

babynames <- babynames |>
  rename(number = n) |>
  select(year,name,number) |>
  group_by(year) |>
  mutate(year_total = sum(number) )

Now you can see the new variable:

summary(babynames)

      year          name               number          year_total     
 Min.   :1880   Length:1924665     Min.   :    5.0   Min.   : 192696  
 1st Qu.:1951   Class :character   1st Qu.:    7.0   1st Qu.:3040409  
 Median :1985   Mode  :character   Median :   12.0   Median :3646362  
 Mean   :1975                      Mean   :  180.9   Mean   :3254023  
 3rd Qu.:2003                      3rd Qu.:   32.0   3rd Qu.:3799172  
 Max.   :2017                      Max.   :99686.0   Max.   :4200007  

  • Related