我有九個相同資料框的串列 waterlevels
,其中包含有關滲壓計 K01 到 K09 的資訊。這些資料幀中的每一個都包含 96860 行,它們都有 13 個變數,除了一個有 21 個變數(這些額外的列并不重要)。
$K01
dateandtime seconds pressure_kPa temperature_.C baropressure_kpa barocompensation_kpa waterlevel_cm lengthcables_cm
1 2019-11-05 00:00:00 0 111.482 13.261 98.854 12.628 128.7697 490
2 2019-11-05 00:15:00 900 111.506 13.261 98.883 12.623 128.7188 490
3 2019-11-05 00:30:00 1800 111.511 13.261 98.872 12.639 128.8819 490
4 2019-11-05 00:45:00 2700 111.544 13.261 98.898 12.646 128.9533 490
5 2019-11-05 01:00:00 3600 111.536 13.313 98.913 12.623 128.7188 490
我想將此串列融合到一個僅包含一個變數barocompensation_kpa
的資料幀和dateandtime
使用以下代碼的變數:
waterlevels_all <- melt(waterlevels, id.vars=c("dateandtime", "barocompensation_kpa"))
但是結果資料waterlevels_all
框有 10364020 個觀察值,而只有這個 df 應該有 871740 行(9*96860)。所以它必須有 3 個變數:dateandtime
, barocompensation_kpa
并且L1
是相應資料框的名稱(= 滲壓計的名稱 - K01 到 K09)。
> head(waterlevels_all)
dateandtime barocompensation_kpa variable value L1
1 2019-11-05 00:00:00 17.55336 seconds 0 K01
2 2019-11-05 00:15:00 17.55489 seconds 900 K01
3 2019-11-05 00:30:00 17.55703 seconds 1800 K01
4 2019-11-05 00:45:00 17.55347 seconds 2700 K01
5 2019-11-05 01:00:00 17.55540 seconds 3600 K01
6 2019-11-05 01:15:00 17.56050 seconds 4500 K01
這怎么可能?如何獲得正確的資料框?
uj5u.com熱心網友回復:
以下是一些選項tidyverse
- 回圈list
使用感興趣imap
的select
列,并使用串列元素名稱 ( .y
)創建新列“L1”
library(dplyr)
library(purrr)
imap_dfr(waterlevels, ~ .x %>%
select(dateandtime, barocompensation_kpa) %>%
mutate(L1 = .y))
或使用map
,僅select
列并使用.id
(_dfr
通過 rbinding 串列元素回傳單個資料集)創建新列
map_dfr(waterlevels, ~ .x %>%
select(dateandtime, barocompensation_kpa), .id = "L1")
uj5u.com熱心網友回復:
嘗試lapply
使用后續的Map
and rbind
。
lapply(lst, subset, select=c('dateandtime', 'barocompensation_kpa')) |>
Map(f=`[<-`, 'L1', value=names(lst)) |>
# c(make.row.names=FALSE) |> ## optional, to reset row names
do.call(what=rbind)
# dateandtime barocompensation_kpa L1
# K01.1 0 0 K01
# K01.2 0 0 K01
# K01.3 0 0 K01
# K02.1 0 0 K02
# K02.2 0 0 K02
# K02.3 0 0 K02
# K03.1 0 0 K03
# K03.2 0 0 K03
# K03.3 0 0 K03
資料:
lst <- list(K01 = structure(list(dateandtime = c(0, 0, 0), seconds = c(0,
0, 0), pressure_kPa = c(0, 0, 0), temperature_.C = c(0, 0, 0),
baropressure_kpa = c(0, 0, 0), barocompensation_kpa = c(0,
0, 0), waterlevel_cm = c(0, 0, 0), lengthcables_cm = c(0,
0, 0)), row.names = c(NA, -3L), class = "data.frame"), K02 = structure(list(
dateandtime = c(0, 0, 0), seconds = c(0, 0, 0), pressure_kPa = c(0,
0, 0), temperature_.C = c(0, 0, 0), baropressure_kpa = c(0,
0, 0), barocompensation_kpa = c(0, 0, 0), waterlevel_cm = c(0,
0, 0), lengthcables_cm = c(0, 0, 0)), row.names = c(NA, -3L
), class = "data.frame"), K03 = structure(list(dateandtime = c(0,
0, 0), seconds = c(0, 0, 0), pressure_kPa = c(0, 0, 0), temperature_.C = c(0,
0, 0), baropressure_kpa = c(0, 0, 0), barocompensation_kpa = c(0,
0, 0), waterlevel_cm = c(0, 0, 0), lengthcables_cm = c(0, 0,
0), X9 = c(0, 0, 0), X10 = c(0, 0, 0), X11 = c(0, 0, 0), X12 = c(0,
0, 0), X13 = c(0, 0, 0), X14 = c(0, 0, 0), X15 = c(0, 0, 0),
X16 = c(0, 0, 0), X17 = c(0, 0, 0), X18 = c(0, 0, 0), X19 = c(0,
0, 0), X20 = c(0, 0, 0), X21 = c(0, 0, 0)), class = "data.frame", row.names = c(NA,
-3L)))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/505719.html
上一篇:XML文本閱讀器覆寫跳過子元素
下一篇:從串列中洗掉重復的子字串