Home > Blockchain >  using a function to add a column to dataframe in R
using a function to add a column to dataframe in R

Time:01-27

I'm trying to use a function to add a column to a dataframe in R. The function would simply concatenate existing fields in the dataframe, and add the concatenated values as a new column:

year <- c(2017, 2018, 2019, 2020, 2017, 2018, 2019, 2020, 2017, 2018, 2019, 2020, 2017, 2018, 2019, 2020)
drugType <- c('drug1', 'drug1', 'drug1', 'drug1', 'drug2', 'drug2', 'drug2', 'drug2', 'drug3', 'drug3', 'drug3', 'drug3', 'drug4', 'drug4', 'drug4', 'drug4')
total <- c(21, 18, 17, 10, 1, 1, 3, 4, 192, 242, 111, 234, 34, 26, 36, 17)
perCapitaRate <- c(1.4, 4.3, 3.4, 3.0, 23.0, 3.3, 3.4, 3.5, 3.6, 45.4, 4.4, 4.5, 23.6, 34.7, 22.3, 2.0)
drugDeathsByYr <- data.frame(year, drugType, total, perCapitaRate)



concat_fields <- function(df) {
  df$year_drug <- paste(df$year, df$drugType, sep="_")
  
}

concat_fields(drugDeathsByYr)

However, when I run this, the df drugDeathsByYr does not have the added column.

Thanks.

CodePudding user response:

It is just that the return is the assignment and not the data

concat_fields <- function(df) {
  df$year_drug <- paste(df$year, df$drugType, sep="_")
  df
  
}

and now run

drugDeathsByYr <- concat_fields(drugDeathsByYr)
> drugDeathsByYr
   year drugType total perCapitaRate  year_drug
1  2017    drug1    21           1.4 2017_drug1
2  2018    drug1    18           4.3 2018_drug1
3  2019    drug1    17           3.4 2019_drug1
4  2020    drug1    10           3.0 2020_drug1
5  2017    drug2     1          23.0 2017_drug2
6  2018    drug2     1           3.3 2018_drug2
7  2019    drug2     3           3.4 2019_drug2
8  2020    drug2     4           3.5 2020_drug2
9  2017    drug3   192           3.6 2017_drug3
10 2018    drug3   242          45.4 2018_drug3
11 2019    drug3   111           4.4 2019_drug3
12 2020    drug3   234           4.5 2020_drug3
13 2017    drug4    34          23.6 2017_drug4
14 2018    drug4    26          34.7 2018_drug4
15 2019    drug4    36          22.3 2019_drug4
16 2020    drug4    17           2.0 2020_drug4

If we want to update the original object in the global env, use an envir as argument and update

concat_fields <- function(df, envir = .GlobalEnv) {
  tmp <- deparse(substitute(df))
  envir[[tmp]]$year_drug <- paste(df$year, df$drugType, sep="_")
  
}

concat_fields(drugDeathsByYr)

-output

> drugDeathsByYr
   year drugType total perCapitaRate  year_drug
1  2017    drug1    21           1.4 2017_drug1
2  2018    drug1    18           4.3 2018_drug1
3  2019    drug1    17           3.4 2019_drug1
4  2020    drug1    10           3.0 2020_drug1
5  2017    drug2     1          23.0 2017_drug2
6  2018    drug2     1           3.3 2018_drug2
7  2019    drug2     3           3.4 2019_drug2
8  2020    drug2     4           3.5 2020_drug2
9  2017    drug3   192           3.6 2017_drug3
10 2018    drug3   242          45.4 2018_drug3
11 2019    drug3   111           4.4 2019_drug3
12 2020    drug3   234           4.5 2020_drug3
13 2017    drug4    34          23.6 2017_drug4
14 2018    drug4    26          34.7 2018_drug4
15 2019    drug4    36          22.3 2019_drug4
16 2020    drug4    17           2.0 2020_drug4
  •  Tags:  
  • r
  • Related