转换多个数据框的相同列
Convert identical column of multiple dataframes
我在全局环境中有许多数据帧,我们称它们为 a
、b
和 c
。
每个数据帧都有一个名为 start_time
的列,需要将其转换为 posix class,但我正在寻找无需编写相同代码即可完成此操作的方法对于每个数据帧。代码是:
a$start_time <- strptime(a$start_time, format = '%Y-%m-%d %H:%M:%S')
这只会将 start_time
转换为 a
使用数据帧名称,如何设计一种方法来遍历每个数据帧并将 start_time
转换为 posix?
这种 lapply
的尝试仅适用于第一个数据帧...
ll <- list(a, b, c)
lapply(ll,function(df){
df$start_time <- strptime(df$start_time, format = '%Y-%m-%d %H:%M:%S')
})
数据:df1
、df2
、df3
df1 <- data.frame(start_time = seq(Sys.time(), Sys.time() + 100, 10))
df2 <- data.frame(start_time = seq(Sys.time(), Sys.time() + 100, 10))
df3 <- data.frame(start_time = seq(Sys.time(), Sys.time() + 100, 10))
# create a vector with names of the data frames
data_vec <- c('df1', 'df2', 'df3')
# loop through the data_vec and modify the start_time column
a1 <- lapply(data_vec, function( x ) {
x <- get( x )
x <- within(x, start_time <- strptime(start_time, format = '%Y-%m-%d %H:%M:%S') )
return( x )
})
# assign names to the modified data in a1
names(a1) <- data_vec
# list objects in global environment
ls()
# [1] "a1" "data_vec" "df1" "df2" "df3"
# remove df1, df2, df3 from global environment
rm(list = c('df1', 'df2', 'df3') )
# confirm the removal of data
ls()
# [1] "a1" "data_vec"
# assign the named list in a1 as data in global environment
list2env(a1, envir = .GlobalEnv)
# list objects in global environment and confirm that the data appeared again
ls()
# [1] "a1" "data_vec" "df1" "df2" "df3"
# output
head(df1)
# start_time
# 1 2017-03-03 22:49:54
# 2 2017-03-03 22:50:04
# 3 2017-03-03 22:50:14
# 4 2017-03-03 22:50:24
# 5 2017-03-03 22:50:34
# 6 2017-03-03 22:50:44
head(df2)
# start_time
# 1 2017-03-03 22:49:54
# 2 2017-03-03 22:50:04
# 3 2017-03-03 22:50:14
# 4 2017-03-03 22:50:24
# 5 2017-03-03 22:50:34
# 6 2017-03-03 22:50:44
在 OP 的代码中,没有返回数据集。所以,基本上就是
lapply(ll,function(df){
df$start_time <- strptime(df$start_time, format = '%Y-%m-%d %H:%M:%S')
df
})
但是,在不返回对象和匿名函数调用的情况下,transform
是一个选项。另外,strptime
returns POSIXlt
class 也是。如果我们只需要 POSIXct
,请使用 as.POSIXct
lapply(ll, transform, start_time = as.POSIXct(start_time, format = '%Y-%m-%d %H:%M:%S'))
或者让它更紧凑
library(lubridate)
lapply(ll, transform, start_time = ymd_hms(start_time))
我在全局环境中有许多数据帧,我们称它们为 a
、b
和 c
。
每个数据帧都有一个名为 start_time
的列,需要将其转换为 posix class,但我正在寻找无需编写相同代码即可完成此操作的方法对于每个数据帧。代码是:
a$start_time <- strptime(a$start_time, format = '%Y-%m-%d %H:%M:%S')
这只会将 start_time
转换为 a
使用数据帧名称,如何设计一种方法来遍历每个数据帧并将 start_time
转换为 posix?
这种 lapply
的尝试仅适用于第一个数据帧...
ll <- list(a, b, c)
lapply(ll,function(df){
df$start_time <- strptime(df$start_time, format = '%Y-%m-%d %H:%M:%S')
})
数据:df1
、df2
、df3
df1 <- data.frame(start_time = seq(Sys.time(), Sys.time() + 100, 10))
df2 <- data.frame(start_time = seq(Sys.time(), Sys.time() + 100, 10))
df3 <- data.frame(start_time = seq(Sys.time(), Sys.time() + 100, 10))
# create a vector with names of the data frames
data_vec <- c('df1', 'df2', 'df3')
# loop through the data_vec and modify the start_time column
a1 <- lapply(data_vec, function( x ) {
x <- get( x )
x <- within(x, start_time <- strptime(start_time, format = '%Y-%m-%d %H:%M:%S') )
return( x )
})
# assign names to the modified data in a1
names(a1) <- data_vec
# list objects in global environment
ls()
# [1] "a1" "data_vec" "df1" "df2" "df3"
# remove df1, df2, df3 from global environment
rm(list = c('df1', 'df2', 'df3') )
# confirm the removal of data
ls()
# [1] "a1" "data_vec"
# assign the named list in a1 as data in global environment
list2env(a1, envir = .GlobalEnv)
# list objects in global environment and confirm that the data appeared again
ls()
# [1] "a1" "data_vec" "df1" "df2" "df3"
# output
head(df1)
# start_time
# 1 2017-03-03 22:49:54
# 2 2017-03-03 22:50:04
# 3 2017-03-03 22:50:14
# 4 2017-03-03 22:50:24
# 5 2017-03-03 22:50:34
# 6 2017-03-03 22:50:44
head(df2)
# start_time
# 1 2017-03-03 22:49:54
# 2 2017-03-03 22:50:04
# 3 2017-03-03 22:50:14
# 4 2017-03-03 22:50:24
# 5 2017-03-03 22:50:34
# 6 2017-03-03 22:50:44
在 OP 的代码中,没有返回数据集。所以,基本上就是
lapply(ll,function(df){
df$start_time <- strptime(df$start_time, format = '%Y-%m-%d %H:%M:%S')
df
})
但是,在不返回对象和匿名函数调用的情况下,transform
是一个选项。另外,strptime
returns POSIXlt
class 也是。如果我们只需要 POSIXct
,请使用 as.POSIXct
lapply(ll, transform, start_time = as.POSIXct(start_time, format = '%Y-%m-%d %H:%M:%S'))
或者让它更紧凑
library(lubridate)
lapply(ll, transform, start_time = ymd_hms(start_time))