Home > other >  R Creating n columns with same calculation but parameter = n
R Creating n columns with same calculation but parameter = n

Time:04-07

I would like to illustrate a series of calculations with a curvature variation. That variation is set through a parameter. This is an example of what I'm doing:

param_1 = 1
param_2 = 2
param_n = 10

df <- tibble(x = c(1:10))


# A tibble: 10 x 1
       x
   <int>
 1     1
 2     2
 3     3
 4     4
 5     5
 6     6
 7     7
 8     8
 9     9
10    10

df %>%
  mutate(var_1 = (1 - x) / (1   (x * param_1)),
         var_2 = (1 - x) / (1   (x * param_2)),
         var_n = (1 - x) / (1   (x * param_n)))


# A tibble: 10 x 4
       x  var_1  var_2   var_n
   <int>  <dbl>  <dbl>   <dbl>
 1     1  0      0      0     
 2     2 -0.333 -0.2   -0.0476
 3     3 -0.5   -0.286 -0.0645
 4     4 -0.6   -0.333 -0.0732
 5     5 -0.667 -0.364 -0.0784
 6     6 -0.714 -0.385 -0.0820
 7     7 -0.75  -0.4   -0.0845
 8     8 -0.778 -0.412 -0.0864
 9     9 -0.8   -0.421 -0.0879
10    10 -0.818 -0.429 -0.0891

I'm essentially looking for a shorter way to do the same analysis say from 1 to 20, or 1 to 50, without having to manually write 50 parameters and 50 variable calculations. I found some solutions which involve creating many dataframes, but I was hoping for a simpler solution which would automatically create the data columns from a range.

CodePudding user response:

You can use map and unnest_wider() like this

tibble(x=1:10) %>%
  mutate(var = map(x,~(1-.x)/(1 .x*(1:50)))) %>%
  unnest_wider(var,names_sep = "")

Output:

# A tibble: 10 x 51
       x   var1   var2   var3   var4    var5    var6    var7    var8    var9   var10   var11   var12   var13   var14   var15   var16   var17   var18
   <int>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
 1     1  0      0      0      0      0       0       0       0       0       0       0       0       0       0       0       0       0       0     
 2     2 -0.333 -0.2   -0.143 -0.111 -0.0909 -0.0769 -0.0667 -0.0588 -0.0526 -0.0476 -0.0435 -0.04   -0.0370 -0.0345 -0.0323 -0.0303 -0.0286 -0.0270
 3     3 -0.5   -0.286 -0.2   -0.154 -0.125  -0.105  -0.0909 -0.08   -0.0714 -0.0645 -0.0588 -0.0541 -0.05   -0.0465 -0.0435 -0.0408 -0.0385 -0.0364
 4     4 -0.6   -0.333 -0.231 -0.176 -0.143  -0.12   -0.103  -0.0909 -0.0811 -0.0732 -0.0667 -0.0612 -0.0566 -0.0526 -0.0492 -0.0462 -0.0435 -0.0411
 5     5 -0.667 -0.364 -0.25  -0.190 -0.154  -0.129  -0.111  -0.0976 -0.0870 -0.0784 -0.0714 -0.0656 -0.0606 -0.0563 -0.0526 -0.0494 -0.0465 -0.0440
 6     6 -0.714 -0.385 -0.263 -0.2   -0.161  -0.135  -0.116  -0.102  -0.0909 -0.0820 -0.0746 -0.0685 -0.0633 -0.0588 -0.0549 -0.0515 -0.0485 -0.0459
 7     7 -0.75  -0.4   -0.273 -0.207 -0.167  -0.140  -0.12   -0.105  -0.0938 -0.0845 -0.0769 -0.0706 -0.0652 -0.0606 -0.0566 -0.0531 -0.05   -0.0472
 8     8 -0.778 -0.412 -0.28  -0.212 -0.171  -0.143  -0.123  -0.108  -0.0959 -0.0864 -0.0787 -0.0722 -0.0667 -0.0619 -0.0579 -0.0543 -0.0511 -0.0483
 9     9 -0.8   -0.421 -0.286 -0.216 -0.174  -0.145  -0.125  -0.110  -0.0976 -0.0879 -0.08   -0.0734 -0.0678 -0.0630 -0.0588 -0.0552 -0.0519 -0.0491
10    10 -0.818 -0.429 -0.290 -0.220 -0.176  -0.148  -0.127  -0.111  -0.0989 -0.0891 -0.0811 -0.0744 -0.0687 -0.0638 -0.0596 -0.0559 -0.0526 -0.0497
# ... with 32 more variables: var19 <dbl>, var20 <dbl>, var21 <dbl>, var22 <dbl>, var23 <dbl>, var24 <dbl>, var25 <dbl>, var26 <dbl>, var27 <dbl>,
#   var28 <dbl>, var29 <dbl>, var30 <dbl>, var31 <dbl>, var32 <dbl>, var33 <dbl>, var34 <dbl>, var35 <dbl>, var36 <dbl>, var37 <dbl>, var38 <dbl>,
#   var39 <dbl>, var40 <dbl>, var41 <dbl>, var42 <dbl>, var43 <dbl>, var44 <dbl>, var45 <dbl>, var46 <dbl>, var47 <dbl>, var48 <dbl>, var49 <dbl>,
#   var50 <dbl>

CodePudding user response:

In base R, use sapply

df <- tibble(x = c(1:10))
param = 1:10

df <- cbind(df, sapply(param, \(y) (1 - df$x) / (1   (df$x * y))))
colnames(df)[-1] <- paste0("var_", 1:10)

    x      var_1      var_2      var_3      var_4       var_5       var_6       var_7       var_8       var_9      var_10
1   1  0.0000000  0.0000000  0.0000000  0.0000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000  0.00000000
2   2 -0.3333333 -0.2000000 -0.1428571 -0.1111111 -0.09090909 -0.07692308 -0.06666667 -0.05882353 -0.05263158 -0.04761905
3   3 -0.5000000 -0.2857143 -0.2000000 -0.1538462 -0.12500000 -0.10526316 -0.09090909 -0.08000000 -0.07142857 -0.06451613
4   4 -0.6000000 -0.3333333 -0.2307692 -0.1764706 -0.14285714 -0.12000000 -0.10344828 -0.09090909 -0.08108108 -0.07317073
5   5 -0.6666667 -0.3636364 -0.2500000 -0.1904762 -0.15384615 -0.12903226 -0.11111111 -0.09756098 -0.08695652 -0.07843137
6   6 -0.7142857 -0.3846154 -0.2631579 -0.2000000 -0.16129032 -0.13513514 -0.11627907 -0.10204082 -0.09090909 -0.08196721
7   7 -0.7500000 -0.4000000 -0.2727273 -0.2068966 -0.16666667 -0.13953488 -0.12000000 -0.10526316 -0.09375000 -0.08450704
8   8 -0.7777778 -0.4117647 -0.2800000 -0.2121212 -0.17073171 -0.14285714 -0.12280702 -0.10769231 -0.09589041 -0.08641975
9   9 -0.8000000 -0.4210526 -0.2857143 -0.2162162 -0.17391304 -0.14545455 -0.12500000 -0.10958904 -0.09756098 -0.08791209
10 10 -0.8181818 -0.4285714 -0.2903226 -0.2195122 -0.17647059 -0.14754098 -0.12676056 -0.11111111 -0.09890110 -0.08910891
  • Related