[英]Melt dataframes of two different lists into one list of data frames in r
[英]Melt list of identical dataframes to one dataframe in R
我有九個相同數據框的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
我想使用以下代碼將此列表融合為一個 dataframe 僅包含一個變量barocompensation_kpa
和dateandtime
變量:
waterlevels_all <- melt(waterlevels, id.vars=c("dateandtime", "barocompensation_kpa"))
但隨后生成的 dataframe waterlevels_all
有 10364020 個觀測值,而只有這個 df 應該有 871740 行 (9*96860)。 所以它必須有 3 個變量: dateandtime
、 barocompensation_kpa
和L1
是相應 dataframe 的名稱(= 滲壓計的名稱 - 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
這怎么可能? 如何獲得正確的 dataframe?
以下是tidyverse
的一些選項 - 使用imap
循環list
, select
感興趣的列,並使用列表元素名稱( .y
)創建新列“L1”
library(dplyr)
library(purrr)
imap_dfr(waterlevels, ~ .x %>%
select(dateandtime, barocompensation_kpa) %>%
mutate(L1 = .y))
或者使用map
,只需select
列並使用.id
創建新列( _dfr
通過綁定列表元素返回單個數據集)
map_dfr(waterlevels, ~ .x %>%
select(dateandtime, barocompensation_kpa), .id = "L1")
嘗試lapply
與隨后的Map
和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)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.