假设这是我的数据集
我想考虑始终相同的模式来计算一个新变量(突变)。因此,新变量必须是一列的平均值,且前两列为空格,依此类推。 [在实际数据集中,我要取平均值的两列之间有30列]。
在excel中,选择两个变量后,将在右侧进行“拖放”。因此,当结果变为“丢失”时,算法应停止。
我想留在整洁的环境中。 有什么建议么?
码:
ds <-structure(list(identificacao = c("3004U", "77584X", "25917G",
"39895C", "20597Y", "64085M", "51573F", "42221E", "58658E", "8983C",
"18516K", "27050E"), lh_aparc_volume = c(2112, 2081, 2050, 2350,
2250, 1730, 1874, 1821, 2004, 1928, 1844, 2900), lh_bankssts_volume = c(1750,
1654, 1344, 1876, 1366, 1424, 1416, 1521, 1231, 2415, 938, 1356
), rh_aparc_volume = c(1797, 1895, 1386, 1875, 2123, 1457, 1754,
2478, 1670, 1613, 1702, 1873), rh_bankssts_volume = c(1951, 1991,
1774, 2539, 1830, 2564, 2433, 1092, 1803, 2009, 1609, 1787)), row.names = c(NA,
-12L), class = c("tbl_df", "tbl", "data.frame"))
ds
Here's an approach with
bind_cols
andmap2
:Another "tidyverse" approach would be
tidyr:pivot_longer
:Obviously this moves whatever
lh
andrh
are to the end of the column name. If this is a dealbreaker, you could userename_at
.简单变异有什么问题?