我正在从我的计算机上读取文件列表,并使用purrr和dplyr对它们进行多次转换,一切都很好,但是我有一个带有每个数据框的ID的向量,我想要添加一个列每个数据框的数据ID。
加载库library(readr)
library(lubridate)
library(dplyr)
library(purrr)
阅读要修改的文件列表
ArchivosTemp <- list.files(pattern = "Tem.csv")
假设在代码的第一行之后发生了名为Temperaturas的数据帧列表
Temperaturas <- list(structure(list(`Date/Time` = c("01-07-2016 14:55", "01-07-2016 15:55",
"01-07-2016 16:55", "01-07-2016 17:55", "01-07-2016 18:55", "01-07-2016 19:55"
), Unit = c("C", "C", "C", "C", "C", "C"), Value = c(28L, 24L,
25L, 25L, 25L, 25L), a = c(68L, 682L, 182L, 182L, 182L, 182L)), .Names = c("Date/Time",
"Unit", "Value", "a"), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame")), structure(list(`Date/Time` = c("12-06-2016 19:44",
"12-06-2016 20:44", "12-06-2016 21:44", "12-06-2016 22:44", "12-06-2016 23:44",
"13-06-2016 0:44"), Unit = c("C", "C", "C", "C", "C", "C"), Value = c(31L,
29L, 27L, 26L, 26L, 24L), a = c(129L, 131L, 632L, 633L, 133L,
633L)), .Names = c("Date/Time", "Unit", "Value", "a"), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
`Date/Time` = c("07-06-16 7:54:01", "07-06-16 8:54:01", "07-06-16 9:54:01",
"07-06-16 10:54:01", "07-06-16 11:54:01", "07-06-16 12:54:01"
), Unit = c("C", "C", "C", "C", "C", "C"), Value = c(23L,
19L, 25L, 27L, 30L, 34L), a = c("119", "116", "119", "119",
"118", "113")), .Names = c("Date/Time", "Unit", "Value",
"a"), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
)))
以及具有列表中每个元素的ID的向量
IDs <- c("H1F102", "H1F105", "H1F106")
a <- ArchivosTemp %>% map(read_csv) %>% map(~rename(.x, Temperatura = Value, Date.Time = `Date/Time`)) %>% map(~mutate(.x, Date.Time = dmy_hms(Date.Time))) %>% map(~select(.x, Date.Time, Temperatura))
由于您无法从mu计算机读取csvs,请将ArchivosTemp %>% map(read_csv)
替换为我在上面列出的列表
a <- Temperaturas %>% map(~rename(.x, Temperatura = Value, Date.Time = `Date/Time`)) %>% map(~mutate(.x, Date.Time = dmy_hms(Date.Time))) %>% map(~select(.x, Date.Time, Temperatura))
然后我希望3个数据帧中的每一个都有一个名为ID的列,其ID值向量中的相应元素我试过这个:
a <- Temperaturas %>% map(~rename(.x, Temperatura = Value, Date.Time = `Date/Time`)) %>% map(~mutate(.x, Date.Time = dmy_hms(Date.Time))) %>% map(~select(.x, Date.Time, Temperatura)) %>% map2(y = IDs,~mutate(.x, ID = y.))
但它不起作用,我做错了什么想法?
作为一个例子,这是我期望仅使用第一个数据框
的结果a <- Temperaturas %>% map(~rename(.x, Temperatura = Value, Date.Time = `Date/Time`)) %>% map(~mutate(.x, Date.Time = dmy_hms(Date.Time))) %>% map(~select(.x, Date.Time, Temperatura)) %>% reduce(rbind)
mutate(a[[1]], ID = IDs[1])
变成
# A tibble: 6 x 3
Date.Time Temperatura ID
<dttm> <int> <chr>
1 2020-07-01 16:14:55 28 H1F102
2 2020-07-01 16:15:55 24 H1F102
3 2020-07-01 16:16:55 25 H1F102
4 2020-07-01 16:17:55 25 H1F102
5 2020-07-01 16:18:55 25 H1F102
6 2020-07-01 16:19:55 25 H1F102
答案 0 :(得分:3)
map2
存在次要参数问题,参数名称为.x
,.y
,将y
更改为.y
对我有用:< / p>
map2(.y = IDs, ~ mutate(.x, ID = .y))
此外,如果您最终需要将列表中的所有元素作为单个数据框绑定,则可以set_names
使用IDs
向量添加到列表中,然后在.id
参数中指定map_df
参数bind_rows
,它将映射和.id
列表中的所有数据框以形成新的最终数据框,并将列表名称转换为名为Temperaturas %>%
set_names(IDs) %>%
map_df(~ transmute(.x, Date.Time=dmy_hms(`Date/Time`), Temperatura=Value), .id="ID")
# A tibble: 18 x 3
# ID Date.Time Temperatura
# <chr> <dttm> <int>
# 1 H1F102 2020-07-01 16:14:55 28
# 2 H1F102 2020-07-01 16:15:55 24
# 3 H1F102 2020-07-01 16:16:55 25
# 4 H1F102 2020-07-01 16:17:55 25
# 5 H1F102 2020-07-01 16:18:55 25
# 6 H1F102 2020-07-01 16:19:55 25
# 7 H1F105 2020-06-12 16:19:44 31
# 8 H1F105 2020-06-12 16:20:44 29
# 9 H1F105 2020-06-12 16:21:44 27
#10 H1F105 2020-06-12 16:22:44 26
#11 H1F105 2020-06-12 16:23:44 26
#12 H1F105 2020-06-13 16:00:44 24
#13 H1F106 2016-06-07 07:54:01 23
#14 H1F106 2016-06-07 08:54:01 19
#15 H1F106 2016-06-07 09:54:01 25
#16 H1F106 2016-06-07 10:54:01 27
#17 H1F106 2016-06-07 11:54:01 30
#18 H1F106 2016-06-07 12:54:01 34
的新列:< / p>
transmute
此外,您可以使用rename %>% mutate %>% select
作为{{1}}的简写