Home > Net >  Assign value to dynamic column name in a function in R
Assign value to dynamic column name in a function in R

Time:12-04

I have a table:

num1<-runif(10)
num2<-runif(10)
df<-data.frame(num1,num2)
df
         num1      num2
1  0.41170486 0.8198323
2  0.46594131 0.7300186
3  0.09005351 0.9667960
4  0.33373968 0.6827277
5  0.48305910 0.2254627
6  0.23514394 0.3348283
7  0.79450479 0.3101271
8  0.30476853 0.6706657
9  0.53643541 0.9761387
10 0.66568823 0.8444414

And I want to create a percentile column for each of these 2 columns respectively so the table looks like this:

 df
         num1      num2.   num1_percentile   num2_percentile
1  0.41170486 0.8198323    20%                40%
2  0.46594131 0.7300186    xxx               xxx
3  0.09005351 0.9667960    xxx               xxx
4  0.33373968 0.6827277    xxx               xxx
5  0.48305910 0.2254627    xxx               xxx
6  0.23514394 0.3348283    xxx               xxx
7  0.79450479 0.3101271    xxx               xxx
8  0.30476853 0.6706657    xxx               xxx
9  0.53643541 0.9761387    xxx               xxx
10 0.66568823 0.8444414    xxx               xxx

So the percentile column name will be based on the if it's num1 or num2. I have this function where I am trying to assign percentile values to dynamic column names but seems like assign() did not store values in num1_percentile.

create_percentile<-function(num){
  
  breaks=c( quantile(df[[num]], probs =seq(0, 1, 0.2)))
  
  
  
  values<- cut(df[[num]],
               breaks=breaks,labels=c(  '20%','40%', '60%','80%','100%'))
  values[is.na(values)]<-'20%'
  
  print(values)
  
  assign(paste0(df,'_percentile'),values)
  
  df<-cbind(df,paste0(num,'_percentile'))
  
  return(df)
}

create_percentile('num1')

How do I make it work so it can assign values to dynamic column names?

CodePudding user response:

The reason behind assign not storing the value is that you are storing it in assign(paste0(df,'_percentile'),values) where df is a data frame. So while assigning it considers the first element of df to paste it with _percentile. A small correction is instead of df you can use num.

Also this function cbind(df,paste0(num,'_percentile')) won't work as expected because it will store a character value returned from paste command.

You may use the below code to assign the values to dynamic column names

num1 <- runif(10)
num2 <- runif(10)
df <- data.frame(num1, num2)
df

         num1       num2
1  0.78584180 0.61999953
2  0.33916132 0.91999116
3  0.84080205 0.19279828
4  0.03563479 0.04253741
5  0.16633144 0.23509481
6  0.15459396 0.84458053
7  0.99544676 0.08727064
8  0.25060605 0.51908750
9  0.35631173 0.48853416
10 0.50649402 0.64245468

create_percentile <- function(num) {
  breaks <- quantile(df[[num]], probs = seq(0, 1, 0.2))
  values <- cut(df[[num]], breaks = breaks, labels = c('20%', '40%', '60%', '80%', '100%'))
  values[is.na(values)] <- '20%'
  df <- cbind(df, values)
  names(df)[ncol(df)] <- paste0(num, '_percentile')
  return(df)
}

df <- create_percentile('num1')
df <- create_percentile('num2')

Output:

         num1       num2 num1_percentile num2_percentile
1  0.78584180 0.61999953             80%             80%
2  0.33916132 0.91999116             60%            100%
3  0.84080205 0.19279828            100%             40%
4  0.03563479 0.04253741             20%             20%
5  0.16633144 0.23509481             40%             40%
6  0.15459396 0.84458053             20%            100%
7  0.99544676 0.08727064            100%             20%
8  0.25060605 0.51908750             40%             60%
9  0.35631173 0.48853416             60%             60%
10 0.50649402 0.64245468             80%             80%
  •  Tags:  
  • r
  • Related