简体   繁体   English

无法将map2与带有purrr和dplyr的mutate一起使用

[英]Failed to use map2 with mutate with purrr and dplyr

I am reading a list of files form my computer and doing several transformations on them with purrr and dplyr, everything works great, but I have a vector with the IDs of each data frame created, and I want to add a column with the ID of data for each data frame. 我正在阅读计算机中的文件列表,并使用purrr和dplyr对它们进行一些转换,一切正常,但是我有一个带有创建的每个数据帧的ID的向量,并且我想添加一个ID为每个数据帧的数据。

Loading libraries 加载库
ArchivosTemp <- list.files(pattern = "Tem.csv")

Reading list of files to be read and modified 读取要读取和修改的文件列表

 ArchivosTemp <- list.files(pattern = "Tem.csv") 

For reproducible purposes 出于可复制目的

lets say the list of dataframes called Temperaturas made after the first line of the code is 假设代码第一行之后是称为Temperaturas的数据帧列表

IDs <- c("H1F102", "H1F105", "H1F106")

and a vector with the ID of each element of the list 和一个带有列表中每个元素的ID的向量

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))

The purrr code that is working so far 到目前为止有效的purrr代码

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))

Since you cant read the csvs from mu computer lets replace the ArchivosTemp %>% map(read_csv) with the list that I made above 由于您无法从mu电脑读取csv, ArchivosTemp %>% map(read_csv)用我上面创建的列表替换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))  %>% map2(y = IDs,~mutate(.x, ID = y.))

Then I want each of the 3 data frames to have a column called ID with its corresponding element in the IDs vector I tried this: 然后,我希望这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.)) 

but it does not work, any ideas of What I am doing wrong? 但它不起作用,关于我在做什么错的任何想法?

Expected outcome 预期结果

As an example this is the results I expect using only the first data frame 例如,这是我期望仅使用第一个数据帧的结果

# 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

which turns into 变成

 # 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 

You have a minor parameter problem with map2 , the parameters are named as .x , .y , changing y to .y works for me: 您在map2遇到一个较小的参数问题,参数名为.x.y ,将y更改为.y对我有效:

map2(.y = IDs, ~ mutate(.x, ID = .y))

Besides if you eventually need to bind all elements in the list as a single data frame, you can set_names to your list with the IDs vector and then specify the .id parameter in map_df , which will map and bind_rows of all data frames in the lists to form a new final data frame, and converts the list names to a new column with the name of .id : 此外,如果最终需要将列表中的所有元素作为单个数据帧绑定,则可以使用IDs向量将set_names到列表中,然后在map_df指定.id参数,它将映射和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

Besides, you can use transmute as a short hand for rename %>% mutate %>% select 此外,您可以使用transmute作为rename %>% mutate %>% select的简写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM