简体   繁体   English

使用purrr影响列表中每个数据帧的单个列

[英]using purrr to affect single columns of each dataframe in a list

still getting used to purrr and I have one of those questions that I think should be easy, but I don't know how to do it. 仍然习惯于purrr,我有一个我认为应该很容易的问题,但我不知道该怎么做。 All I want to do is convert the datetimes in the below, to dates with as.Date(). 我想要做的就是将下面的日期时间转换为as.Date()的日期。 it's a list of dataframes. 这是一个数据帧列表。 Been playing around but haven't found something that works yet... any help appreciated. 一直在玩,但还没有找到有用的东西...任何帮助赞赏。

df <- data.frame(Date = seq.POSIXt(Sys.time(), Sys.time() + hours(24), by = "hour"),
             useless = "ignore me")
df2 <- data.frame(Date = seq.POSIXt(Sys.time(), Sys.time() + hours(1), by = "min"),
                    useless = "ignore me")
mylist <- list(df,df2)
mylist %<>% map(?????)

The canonical way to achieve your goal would be to combine map with some verb from dplyr , like mutate_at . 实现目标的规范方法是将map与来自dplyr一些动词结合起来,如mutate_at Currently purrr still has the function dmap_at , but it will be removed from purrr in the future. 目前purrr仍然具有功能dmap_at ,但它会从被删除purrr的未来。

Hence, you would map over your list, and then modify the date column with mutate_at : 因此,您将map列表,然后使用mutate_at修改日期列:

library(purrr)
library(lubridate)
library(dplyr)

mylist %>%
  map(~mutate_at(.x, "Date", as.Date))

You could also use at_depth , which in the case of at_depth(1, ...) is equal to map and is therefore not necessary: 你也可以使用at_depth ,在at_depth(1, ...)的情况下,它等于map ,因此不是必需的:

mylist %>%
  at_depth(1, ~mutate_at(.x, "Date", as.Date))

The original approach, staying within purrr , was to use dmap_at : 保持在purrr内的原始方法是使用dmap_at

mylist %>%
  map(~dmap_at(.x, "Date", as.Date))

But since we now have mutate_at and mutate_all and friends, it is recommended to use them instead of dmap , dmap_at and so forth. 但是因为我们现在有mutate_atmutate_all以及朋友,所以建议使用它们而不是dmapdmap_at等等。

Data 数据

df <- data.frame(Date = seq.POSIXt(Sys.time(), Sys.time() + hours(24), by = "hour"),
                 useless = "ignore me")
df2 <- data.frame(Date = seq.POSIXt(Sys.time(), Sys.time() + hours(1), by = "min"),
                  useless = "ignore me")
mylist <- list(df,df2)

You can combine map() with mutate() from the dplyr package (also tidyverse). 您可以将map()与dplyr包中的mutate() (也是tidyverse)结合使用。 map() can be used to apply mutate() each data frame in your list. map()可用于在列表中应用mutate()每个数据框。 mutate() can apply as.Date() to the Date column. mutate()可以将as.Date()应用于Date列。 You'd write it like this: 你这样写:

map(mylist, mutate, Date = as.Date(Date))

This line is saying: 这条线说:

  • map() /apply the mutate() function to each object in mylist map() /将mutate()函数应用于mylist每个对象
  • Each time mutate() is applied to an object, do it as if you were writing mutate(object, Date = as.Date(Date)) 每次将mutate()应用于对象时,就像编写mutate(object, Date = as.Date(Date))

Full code: 完整代码:

library(lubridate)
library(purrr)
library(dplyr)

df <- data.frame(Date = seq.POSIXt(Sys.time(), Sys.time() + hours(24), by = "hour"),
                 useless = "ignore me")
df2 <- data.frame(Date = seq.POSIXt(Sys.time(), Sys.time() + hours(1), by = "min"),
                  useless = "ignore me")
mylist <- list(df,df2)
mylist <- map(mylist, mutate, Date = as.Date(Date))

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

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