当select()
使用参数contains("T")
时,time列丢失。该函数在使用其他字符元素"PAR"
或"RH"
时有效。
这里是我数据子集
#ad_1_top_n <- ad_1[1:3, ]
#dput(ad_1_top_n)
ad_1 <- structure(list(time = 0:2, X1c.PAR1 = c(125.73, 125.76, 125.73
), X1c.PAR2 = c(27.25, 27.25, 27.25), X1c.PAR3 = c(10.03, 10.14,
9.99), X1c.PAR4 = c(0.07, 0.07, 0.07), X1c.T1 = c(21.28, 21.28,
21.27), X1c.T2 = c(20.19, 20.18, 20.18), X1c.T3 = c(19.95, 19.95,
19.95), X1c.T4 = c(20.42, 20.4, 20.4), X1c.RH1 = c(73.57, 73.67,
73.83), X1c.RH2 = c(84.02, 84.04, 84.06), X1c.RH3 = c(82.35,
82.35, 82.36), X1c.RH4 = c(76.94, 76.91, 76.94), X1b.PAR1 = c(108.89,
108.93, 108.93), X1b.PAR2 = c(61.9, 61.9, 61.9), X1b.PAR3 = c(40.1,
40.1, 40.1), X1b.PAR4 = c(0.06, 0.06, 0.1), X1b.T1 = c(20.45,
20.45, 20.46), X1b.T2 = c(20.22, 20.26, 20.26), X1b.T3 = c(20.63,
20.6, 20.61), X1b.T4 = c(20.9, 20.89, 20.89), X1b.RH1 = c(76.2,
76.2, 76.26), X1b.RH2 = c(79.18, 79.21, 79.21), X1b.RH3 = c(75.33,
75.36, 75.47), X1b.RH4 = c(73.52, 73.58, 73.64), X1p.PAR1 = c(144.9,
144.97, 144.94), X1p.PAR2 = c(116.96, 117.03, 116.96), X1p.PAR3 = c(85.2,
85.24, 85.2), X1p.PAR4 = c(2.73, 2.73, 2.73), X1p.T1 = c(20.49,
20.5, 20.48), X1p.T2 = c(20.62, 20.62, 20.61), X1p.T3 = c(19.99,
20, 19.98), X1p.T4 = c(20.31, 20.3, 20.27), X1p.RH1 = c(77.79,
77.87, 77.97), X1p.RH2 = c(77.01, 77.04, 77.05), X1p.RH3 = c(81.93,
82.03, 82.08), X1p.RH4 = c(77.98, 77.75, 77.72), X2c.PAR1 = c(NaN,
NaN, NaN), X2c.PAR2 = c(NaN, NaN, NaN), X2c.PAR3 = c(NaN, NaN,
NaN), X2c.PAR4 = c(NaN, NaN, NaN), X2c.T1 = c(NaN, NaN, NaN),
X2c.T2 = c(NaN, NaN, NaN), X2c.T3 = c(NaN, NaN, NaN), X2c.T4 = c(NaN,
NaN, NaN), X2c.RH1 = c(NaN, NaN, NaN), X2c.RH2 = c(NaN, NaN,
NaN), X2c.RH3 = c(NaN, NaN, NaN), X2c.RH4 = c(NaN, NaN, NaN
), X2b.PAR1 = c(NaN, NaN, NaN), X2b.PAR2 = c(NaN, NaN, NaN
), X2b.PAR3 = c(NaN, NaN, NaN), X2b.PAR4 = c(NaN, NaN, NaN
), X2b.T1 = c(NaN, NaN, NaN), X2b.T2 = c(NaN, NaN, NaN),
X2b.T3 = c(NaN, NaN, NaN), X2b.T4 = c(NaN, NaN, NaN), X2b.RH1 = c(NaN,
NaN, NaN), X2b.RH2 = c(NaN, NaN, NaN), X2b.RH3 = c(NaN, NaN,
NaN), X2b.RH4 = c(NaN, NaN, NaN), X2p.PAR1 = c(NaN, NaN,
NaN), X2p.PAR2 = c(NaN, NaN, NaN), X2p.PAR3 = c(NaN, NaN,
NaN), X2p.PAR4 = c(NaN, NaN, NaN), X2p.T1 = c(NaN, NaN, NaN
), X2p.T2 = c(NaN, NaN, NaN), X2p.T3 = c(NaN, NaN, NaN),
X2p.T4 = c(NaN, NaN, NaN), X2p.RH1 = c(NaN, NaN, NaN), X2p.RH2 = c(NaN,
NaN, NaN), X2p.RH3 = c(NaN, NaN, NaN), X2p.RH4 = c(NaN, NaN,
NaN)), row.names = c(NA, 3L), class = "data.frame")
这里是函数,第三个函数导致了问题:
library(tidyr)
adl_1_PAR_pivot <- ad_1 %>% select(time, contains("PAR")) %>%
pivot_longer(cols = contains("PAR"), names_to = "key", values_to = "PAR") %>%
separate(col = "key", into = c("Dummy1", "Location", "Position", "Dummy2", "Height"),
sep = c(1,2,3,7), remove = TRUE) %>%
select(-Dummy1, -Dummy2)
adl_1_RH_pivot <- ad_1 %>% select(time, contains("RH")) %>%
pivot_longer(cols = contains("RH"), names_to = "key", values_to = "RH") %>%
separate(col = "key", into = c("Dummy1", "Location", "Position", "Dummy2", "Height"),
sep = c(1,2,3,6), remove = TRUE) %>%
select(-Dummy1, -Dummy2)
adl_1_T_pivot <- ad_1 %>% select(time, contains("T")) %>%
pivot_longer(cols = contains("T"), names_to = "key", values_to = "T") %>%
separate(col = "key", into = c("Dummy1", "Location", "Position", "Dummy2", "Height"),
sep = c(1,2,3,5), remove = TRUE) %>%
select(-Dummy1, -Dummy2)
由于某种原因,当使用gather
代替pivot_longer
时,该问题不会发生:
adl_1_T <- ad_1 %>% select(time, contains("T")) %>%
gather(key = key, value = T, -time) %>%
separate(col = "key", into = c("Dummy1","Location", "Position", "Dummy2","Height"),
sep = c(1,2,3,5), remove = TRUE) %>%
select(-Dummy1, -Dummy2)
1条答案
按热度按时间bkkx9g8r1#
注意,
contains()
默认忽略你的值的大小写,所以它也匹配“time”列,你可以看到如果你运行select(ad_1, contains("T"))
,你也会在输出中得到“time”列。要关闭该功能,请使用
contains("T", ignore.case=FALSE)
,例如