用引号重写变量的快速方法
A quick way to rewrite variables with quotations marks
我有这个数据集
> dput(head(data, 10))
structure(list(ID = c("01", "01", "01", "01", "01", "01", "01",
"01", "01", "02"), GR = c("RP", "RP", "RP", "RP", "RP", "RP",
"RP", "RP", "RP", "RP"), SES = c("L", "L", "L", "R", "R", "R",
"V", "V", "V", "L"), COND = c("NEG-CTR", "NEG-NOC", "NEU-NOC",
"NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC",
"NEG-CTR"), `P3(400-450).FCz` = c(-13.733750856001, -11.0706676917232,
-8.4719202003519, -5.04318152659785, -6.25375875400145, -7.0474968134059,
-11.6312151716924, -11.1438413285935, -3.99591470944713, 8.71618523084192
), `P3(400-450).Cz` = c(-9.75024624896264, -8.42427447113084,
-5.61886494275071, 0.73152672768007, 1.11453058993788, -2.25104720773472,
-5.16524399006139, -5.53112490175437, 0.621502123415388, 10.0972150696175
), `P3(400-450).Pz` = c(2.65626156135631, 3.37360619561252, 5.4043480635031,
19.5226358864568, 18.5017390789352, 11.7844254386573, 11.8802266972569,
12.1053426662461, 12.955441582096, 13.9718285231429), `LPPearly(500-700).FCz` = c(-11.2145748677083,
-13.9481657323772, -12.0047623683783, -4.24372139176794, -7.38414465678688,
-6.61221718491095, -11.7785042972793, -9.14927207125904, -7.58190508537766,
14.2438131545118), `LPPearly(500-700).Cz` = c(-8.14861856277773,
-11.7645758007163, -9.16726244915301, 0.918751423320568, -0.674372568935879,
-2.47425935490564, -5.96429031525769, -5.10918437158799, -2.81732229625975,
18.1277616996079), `LPPearly(500-700).Pz` = c(3.4315211013568,
-1.55179922542943, 1.13509020104859, 16.3120391015512, 14.1694182774958,
9.04781409226351, 8.23981597718437, 9.51261484648731, 9.42367409925817,
20.6284861844249), `LPP1(500-1000).FCz` = c(-7.774797181711,
-8.40872060176111, -7.03235376576925, -0.65010821749741, -3.24350263682843,
-2.84832379590422, -5.67295796971287, -4.3918290080777, -2.96652960658775,
12.5228696634748), `LPP1(500-1000).Cz` = c(-5.0379636708446,
-6.36865552801825, -4.54670193355435, 4.05493850786385, 2.67516303169366,
1.02479302810681, -0.774461731301161, -0.650009462761383, 1.14010250644923,
15.0856583318757), `LPP1(500-1000).Pz` = c(4.67200616533014,
1.75888442936359, 2.49276476951357, 13.8997702651793, 13.2135623121441,
9.51479768101391, 9.99385579756163, 11.1212652173052, 10.6989716871958,
15.4011622649207), `LPP2(1000-1500).FCz` = c(-0.397250087672501,
1.01519755373062, -1.17300033366376, 0.663115370014327, -1.72160165493474,
0.0540027107377267, -0.198736254963744, -3.16101041766438, 0.895992279831378,
9.21248347391488), `LPP2(1000-1500).Cz` = c(2.91913936481813,
1.85489998202138, 0.694393606954545, 3.95694572160701, 2.36649557381131,
2.71292196345657, 2.96437294922766, -2.12913230708907, 2.94619035115619,
10.0673617448764), `LPP2(1000-1500).Pz` = c(8.38141924882334,
4.88842858528025, 5.0594399581601, 3.88831364571248, 5.83201076558185,
3.65113189260335, 6.28027312932027, 5.24535230966772, 7.68162285335806,
8.55827491190151), `LPP2(1000-1500).POz` = c(9.61455213192824,
7.21299011910717, 6.1861244061312, 6.46723813995257, 7.25962635499353,
7.92952342178602, 7.91468942320841, 9.94838815736199, 10.2186482048953,
5.76901446016799)), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
>
如您所见,有些变量带有烦人的引号。我编写了这段代码来摆脱它们
data <- data %>%
dplyr::rename("P3(400-450).FCz" = P3Fcz,
"P3(400-450).Cz" = P3cz,
"P3(400-450).Pz" = `P3(400-450).Pz`,
"LPPearly(500-700).FCz" = `LPPearly(500-700).FCz`,
"LPPearly(500-700).Cz" = `LPPearly(500-700).Cz`,
"LPPearly(500-700).Pz" = `LPPearly(500-700).Pz`,
"LPP1(500-1000).FCz" = `LPP1(500-1000).FCz`,
"LPP1(500-1000).Cz" = `LPP1(500-1000).Cz`,
"LPP1(500-1000).Pz" = `LPP1(500-1000).Pz`,
"LPP2(1000-1500).FCz" = `LPP2(1000-1500).FCz`,
"LPP2(1000-1500).Cz" = `LPP2(1000-1500).Cz`,
"LPP2(1000-1500).Pz" = `LPP2(1000-1500).Pz`,
"LPP2(1000-1500).POz" = `LPP2(1000-1500).POz`)
顺便说一下,我很确定可以有一些更快的替代方法来处理它们。你对此有什么建议吗?
提前致谢
你可以这样做:
df %>%
rename_with(~str_remove(.x, "\W.+\W"))
更准确地说:
df %>%
rename_with(~str_remove(.x, "\(.+\."))
我有这个数据集
> dput(head(data, 10))
structure(list(ID = c("01", "01", "01", "01", "01", "01", "01",
"01", "01", "02"), GR = c("RP", "RP", "RP", "RP", "RP", "RP",
"RP", "RP", "RP", "RP"), SES = c("L", "L", "L", "R", "R", "R",
"V", "V", "V", "L"), COND = c("NEG-CTR", "NEG-NOC", "NEU-NOC",
"NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC",
"NEG-CTR"), `P3(400-450).FCz` = c(-13.733750856001, -11.0706676917232,
-8.4719202003519, -5.04318152659785, -6.25375875400145, -7.0474968134059,
-11.6312151716924, -11.1438413285935, -3.99591470944713, 8.71618523084192
), `P3(400-450).Cz` = c(-9.75024624896264, -8.42427447113084,
-5.61886494275071, 0.73152672768007, 1.11453058993788, -2.25104720773472,
-5.16524399006139, -5.53112490175437, 0.621502123415388, 10.0972150696175
), `P3(400-450).Pz` = c(2.65626156135631, 3.37360619561252, 5.4043480635031,
19.5226358864568, 18.5017390789352, 11.7844254386573, 11.8802266972569,
12.1053426662461, 12.955441582096, 13.9718285231429), `LPPearly(500-700).FCz` = c(-11.2145748677083,
-13.9481657323772, -12.0047623683783, -4.24372139176794, -7.38414465678688,
-6.61221718491095, -11.7785042972793, -9.14927207125904, -7.58190508537766,
14.2438131545118), `LPPearly(500-700).Cz` = c(-8.14861856277773,
-11.7645758007163, -9.16726244915301, 0.918751423320568, -0.674372568935879,
-2.47425935490564, -5.96429031525769, -5.10918437158799, -2.81732229625975,
18.1277616996079), `LPPearly(500-700).Pz` = c(3.4315211013568,
-1.55179922542943, 1.13509020104859, 16.3120391015512, 14.1694182774958,
9.04781409226351, 8.23981597718437, 9.51261484648731, 9.42367409925817,
20.6284861844249), `LPP1(500-1000).FCz` = c(-7.774797181711,
-8.40872060176111, -7.03235376576925, -0.65010821749741, -3.24350263682843,
-2.84832379590422, -5.67295796971287, -4.3918290080777, -2.96652960658775,
12.5228696634748), `LPP1(500-1000).Cz` = c(-5.0379636708446,
-6.36865552801825, -4.54670193355435, 4.05493850786385, 2.67516303169366,
1.02479302810681, -0.774461731301161, -0.650009462761383, 1.14010250644923,
15.0856583318757), `LPP1(500-1000).Pz` = c(4.67200616533014,
1.75888442936359, 2.49276476951357, 13.8997702651793, 13.2135623121441,
9.51479768101391, 9.99385579756163, 11.1212652173052, 10.6989716871958,
15.4011622649207), `LPP2(1000-1500).FCz` = c(-0.397250087672501,
1.01519755373062, -1.17300033366376, 0.663115370014327, -1.72160165493474,
0.0540027107377267, -0.198736254963744, -3.16101041766438, 0.895992279831378,
9.21248347391488), `LPP2(1000-1500).Cz` = c(2.91913936481813,
1.85489998202138, 0.694393606954545, 3.95694572160701, 2.36649557381131,
2.71292196345657, 2.96437294922766, -2.12913230708907, 2.94619035115619,
10.0673617448764), `LPP2(1000-1500).Pz` = c(8.38141924882334,
4.88842858528025, 5.0594399581601, 3.88831364571248, 5.83201076558185,
3.65113189260335, 6.28027312932027, 5.24535230966772, 7.68162285335806,
8.55827491190151), `LPP2(1000-1500).POz` = c(9.61455213192824,
7.21299011910717, 6.1861244061312, 6.46723813995257, 7.25962635499353,
7.92952342178602, 7.91468942320841, 9.94838815736199, 10.2186482048953,
5.76901446016799)), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
>
如您所见,有些变量带有烦人的引号。我编写了这段代码来摆脱它们
data <- data %>%
dplyr::rename("P3(400-450).FCz" = P3Fcz,
"P3(400-450).Cz" = P3cz,
"P3(400-450).Pz" = `P3(400-450).Pz`,
"LPPearly(500-700).FCz" = `LPPearly(500-700).FCz`,
"LPPearly(500-700).Cz" = `LPPearly(500-700).Cz`,
"LPPearly(500-700).Pz" = `LPPearly(500-700).Pz`,
"LPP1(500-1000).FCz" = `LPP1(500-1000).FCz`,
"LPP1(500-1000).Cz" = `LPP1(500-1000).Cz`,
"LPP1(500-1000).Pz" = `LPP1(500-1000).Pz`,
"LPP2(1000-1500).FCz" = `LPP2(1000-1500).FCz`,
"LPP2(1000-1500).Cz" = `LPP2(1000-1500).Cz`,
"LPP2(1000-1500).Pz" = `LPP2(1000-1500).Pz`,
"LPP2(1000-1500).POz" = `LPP2(1000-1500).POz`)
顺便说一下,我很确定可以有一些更快的替代方法来处理它们。你对此有什么建议吗?
提前致谢
你可以这样做:
df %>%
rename_with(~str_remove(.x, "\W.+\W"))
更准确地说:
df %>%
rename_with(~str_remove(.x, "\(.+\."))