简体   繁体   English

将值分配给 R 中的 function 中的动态列名

[英]Assign value to dynamic column name in a function in R

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:我想分别为这 2 列中的每一列创建一个百分位列,因此表格如下所示:

 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 .因此,百分位列名称将基于它是num1还是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 .我有这个 function 我试图将百分位值分配给动态列名,但似乎assign()没有将values存储在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?如何使它工作,以便它可以为动态列名分配值?

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. assign不存储值的原因是您将其存储在assign(paste0(df,'_percentile'),values)中,其中 df 是数据框。 So while assigning it considers the first element of df to paste it with _percentile .因此,在分配它时会考虑将 df 的第一个元素与_percentile一起粘贴。 A small correction is instead of df you can use num .您可以使用num来代替df进行小的更正。

Also this function cbind(df,paste0(num,'_percentile')) won't work as expected because it will store a character value returned from paste command.此 function cbind(df,paste0(num,'_percentile'))也不会按预期工作,因为它将存储从paste命令返回的字符值。

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: 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%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM