我(作为R中的初学者)我试图传递一个数据帧列表作为函数的输入,以将一些变量从char更改为日期。当我运行脚本时,它工作。如果我然后在函数中尝试它我没有得到任何错误,但变量的类型仍然是一个字符。您可以在下面找到该功能。提前感谢您的建议。
data <- list(complaints,credit,customers,delivery,subscriptions,formula)
building <- function(x){
for (i in 1:6){
vars <- which(names(x[[i]]) %in% c("StartDate","EndDate","PaymentDate","RenewalDate","ProcessingDate","ComplaintDate","DOB"))
x[[i]][,vars] <- sapply(vars,function(vars) as.Date(x[[i]][,vars],format=f),simplify=FALSE)
}
complaints <- x[[1]]
credit <- x[[2]]
customers <- x[[3]]
delivery <- x[[4]]
subscriptions <- x[[5]]
formula <- x[[6]]
}
building(data)
答案 0 :(得分:1)
您正在尝试修改函数中定义的函数中的对象。这在计算机科学中被称为副作用:http://en.wikipedia.org/wiki/Side_effect_%28computer_science%29
你不能在R中这样做。
相反,您可以这样做,例如:
data <- list(complaints,credit,customers,delivery,subscriptions,formula)
building <- function(x){
for (i in 1:6){
vars <- which(names(x[[i]]) %in% c("StartDate","EndDate","PaymentDate","RenewalDate","ProcessingDate","ComplaintDate","DOB"))
x[[i]][,vars] <- sapply(vars,function(vars) as.Date(x[[i]][,vars],format=f),simplify=FALSE)
}
return(x)
}
output <- building(data)
complaints <- output [[1]]
credit <- output [[2]]
customers <- output [[3]]
delivery <- output [[4]]
subscriptions <- output [[5]]
formula <- output [[6]]