R 使用lappy遍历数据帧列表并更改特定列的类

R 使用lappy遍历数据帧列表并更改特定列的类,r,lapply,R,Lapply,我试图遍历一个有两个数据帧的列表,我想将第2列的类从factor更改为每个数据帧中的数据。我可以用for循环来解决这个问题,但我想学习如何用lappy来解决这个问题 tom <- data.frame(a = c(1,2,3), b = c("2017-01-09","2017-01-10","2017-09-11")) kate <- data.frame(a = c(4,5,6), b = c("2017-01-09","2017-01-10","2017-09-11")) t

我试图遍历一个有两个数据帧的列表,我想将第2列的类从factor更改为每个数据帧中的数据。我可以用for循环来解决这个问题,但我想学习如何用lappy来解决这个问题

tom <- data.frame(a = c(1,2,3), b = c("2017-01-09","2017-01-10","2017-09-11"))
kate <- data.frame(a = c(4,5,6), b = c("2017-01-09","2017-01-10","2017-09-11"))

testList <- list(tom,kate)

f <- lapply(testList, function(x) {
    x[,2] <- as.Date(x[,2])
})

tom我们需要返回'f'中的'x'或
列表
元素

f <- lapply(testList, function(x) {
      x[,2] <- as.Date(x[,2])
      x
})

谢谢两种方法都有效,但第二种方法更好。
f1 <- lapply(testList, transform, b = as.Date(b))