-2

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)

Md Shine
  • 1
  • 1
  • Welcome to Stack Overflow! Please remember that Stack Overflow is not your favourite R forum, but rather a question and answer site for all programming related questions. Thus, please always include the tag of the language you are programming in, that way other users familiar with that language can more easily find your question. Take the [tour] and read up on [ask] to get more information on how this site works, then [edit] the question with the relevant tags. – Adriaan Dec 16 '22 at 15:34
  • `dplyr` has multiple join options: https://dplyr.tidyverse.org/reference/index.html#two-table-verbs – Marcus Dec 16 '22 at 15:41
  • is that possible without use joinig function ? – Md Shine Dec 16 '22 at 15:56

2 Answers2

0

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()
DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25
  • no my data set name was "baby name" & library(dplyr) ,it's shows Error in View : no applicable method for 'rename' applied to an object of class "NULL". my 1st code easliy shows in r view but new cloumn can not save ,2nd code babynames |> select(year,name,number,year_total) |> mutate(fraction_people = number / year_total) |> View() shows number can not find in object – Md Shine Dec 16 '22 at 16:55
  • You will have to provide a snippet of your data for us to be able to help. You can do so using `dput(baby names)` and pasting the output of that in your question. – DaveArmstrong Dec 16 '22 at 16:59
  • now edit my question ,give it. – Md Shine Dec 16 '22 at 17:19
0

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  

George Savva
  • 4,152
  • 1
  • 7
  • 21
  • it's shows Error in View : no applicable method for 'rename' applied to an object of class "NULL" – Md Shine Dec 16 '22 at 16:52
  • i used the babynames dataset from the babynames package. you'll have to adapt the code to your own dataset but the idea is correct, to keep the mutation you need to reassign the object. – George Savva Dec 17 '22 at 19:15