使用for()循环编写用户定义函数

gj3fmq9x  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(106)

这是最小的可重复数据,但我的真实的数据非常庞大。

id <- 1:30
height<-rnorm(30,5.5)
lh_ab<-rnorm(30, 10, 10)
lh_cd<-rnorm(30,10,10)
lh_ef<-rnorm(30,10,10)
rh_ab<-rnorm(30, 10, 10)
rh_cd<-rnorm(30, 10, 10)
rh_ef<-rnorm(30,10,10)

data<-data.frame(id,height,lh_ab,lh_cd,lh_ef,rh_ab,rh_cd,rh_ef)

我想写一个函数,自动计算左半球和右半球的减法得分。
在示例数据集中,我只有三个区域(ab,cd,ef),但在我的真实的数据集中,它是巨大的。

try <- function(data,
              start){ # in this example, it would be 3 because demographic variable ends in # second column 
  
  require(tidyverse)
  
  for{i in start:ncol(data)}
  
  data <- data %>%
    mutate( substraction_ab=lh_ab-rh_ab # this is not good approach because my data is huge. 
           )
  
  return(data)
}

因此,在示例数据集中,我有三个区域(ab,cd,ef),因此该函数应自动生成三个减法分数
(列名应为“substraction_ab”、“substraction_cd”和“substraction_ef”)
然而,我真的不想手动完成,我想自动完成。(同样,我有巨大的数据)

bxjv4tth

bxjv4tth1#

也许像这样?如果你有成千上万的区域,这可能是低效的,但(理论上)用相同的代码容纳任何数量的区域。

library(tidyverse)
left_join(
  data,
  data %>%
    pivot_longer(-(1:2),  names_to = c(".value", "region"), names_pattern = "(.*)_(.*)") %>%
    mutate(subtraction = lh - rh) %>%
    select(-lh, -rh) %>%
    pivot_wider(names_from = region, names_prefix = "subtraction_", values_from = subtraction)
  )

Joining with `by = join_by(id, height)`
   id   height     lh_ab       lh_cd      lh_ef     rh_ab       rh_cd      rh_ef subtraction_ab subtraction_cd subtraction_ef
1   1 4.963743 24.439929  19.8096987 -19.488740  9.156334  26.3810848  -1.011765      15.283595     -6.5713861     -18.476975
2   2 5.386381 13.140046  17.2809355  24.058946  7.904393  -3.7268123  -6.334905       5.235654     21.0077478      30.393851
3   3 5.245852  9.465993  10.3471436  10.465983  4.934789   5.3578854  16.643752       4.531204      4.9892582      -6.177770
4   4 5.563255 14.703032  10.0415088   5.045556  9.394442   9.2967786   8.397739       5.308589      0.7447303      -3.352183
5   5 6.423038  1.074616  16.5844657  10.172173  7.890022   1.2245838  16.149675      -6.815406     15.3598819      -5.977502
6   6 6.878318 -1.068633  23.8303318  20.402625 10.735119  10.6371115  10.535044     -11.803752     13.1932202       9.867581
7   7 5.679026 15.365453   7.5749353   6.299498  7.429691  12.9291548   4.298951       7.935762     -5.3542195       2.000547
8   8 6.569277 16.204336  18.1682924   8.205400 14.910307  15.5521621   6.315595       1.294029      2.6161303       1.889805
9   9 6.353130 17.198668   1.4817886  22.401878  3.057168  15.1165103  -1.746255      14.141500    -13.6347217      24.148133
10 10 4.547916  4.886948  -4.4478230   7.770876 14.061203  -0.8579629  24.284459      -9.174255     -3.5898601     -16.513583
11 11 6.693872  2.883386  11.1011397  19.748252 20.521579  22.0007794  11.833143     -17.638193    -10.8996397       7.915109
12 12 5.772446 13.738622  -3.2722572  32.486037 -4.101585  22.8539110  -5.148017      17.840207    -26.1261681      37.634055
13 13 7.130259 10.199114  11.6999476   5.618982 22.768943  20.7147819   7.960476     -12.569829     -9.0148344      -2.341494
14 14 6.124085 18.252046   8.7890280  32.614859  6.969580   5.3934201  14.695471      11.282466      3.3956080      17.919388
15 15 5.764553 12.513400  12.8205164  16.685103 17.783697 -10.7141924  11.632478      -5.270297     23.5347088       5.052625
16 16 3.893378 18.991748  11.2898280  14.876019 13.881162  20.9814177   4.487271       5.110587     -9.6915897      10.388748
17 17 3.596690 32.280274 -13.0037513   9.932050  9.283911  12.5570437   6.558328      22.996363    -25.5607950       3.373722
18 18 4.617749 31.976760  12.4222525  15.287380  3.235324  17.0409190  27.785597      28.741435     -4.6186665     -12.498217
19 19 4.485808 -2.691635  30.8323614  -4.922267 -3.751266  16.5484166  11.235567       1.059630     14.2839449     -16.157835
20 20 4.222245  1.767996   8.3993945  23.982754  9.881491  13.3809699   6.048850      -8.113495     -4.9815754      17.933904
21 21 5.871159  1.677264  41.2457134  28.925011 -2.397890   3.7362755  24.455750       4.075154     37.5094379       4.469261
22 22 4.084040 19.460378  20.4776372  19.300892 14.309755   5.7580247  -8.003895       5.150622     14.7196125      27.304786
23 23 5.055963 -6.966262  18.2448080  14.892892 10.280682  17.1150449  21.261742     -17.246944      1.1297631      -6.368850
24 24 5.083962  7.827298  37.8725779  20.203090 12.557794  30.2945982  12.748429      -4.730496      7.5779796       7.454661
25 25 4.110982 -4.571252  12.7857379  18.488348 32.517282  20.7328298  -1.681375     -37.088534     -7.9470919      20.169724
26 26 7.403822 -1.036414   8.9284757  15.904053 21.842934   4.7302480   7.176483     -22.879349      4.1982277       8.727570
27 27 5.122484 24.320455  -0.4545514  21.093332 22.283264   4.1424711   2.763517       2.037191     -4.5970225      18.329815
28 28 4.486269 15.334323  17.9872966  12.676585  1.835297  21.2356419  23.988674      13.499025     -3.2483453     -11.312089
29 29 7.816775 34.104426  24.4353727   6.979795 -4.217921  10.0241760 -19.565703      38.322346     14.4111966      26.545499
30 30 6.917879 -2.139813  31.6188706  21.421873 11.270391  23.9528301  29.831335     -13.410204      7.6660405      -8.409463

相关问题