使用dataframe进行聚集在lappy之后的行为不同
当我应用使用dataframe进行聚集在lappy之后的行为不同,r,dplyr,lapply,R,Dplyr,Lapply,当我应用lappy时,我的数据集和gather(来自dplyr)中的某些更改不起作用。下面是一个最低限度的工作示例: employee<- c('John','Peter','Mary',"Kate") salarypre <- c(-1,23.5,33.2,34) salarypost <- c(51,25.2,53,24.3) startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14','2008-3-12')
lappy
时,我的数据集和gather
(来自dplyr
)中的某些更改不起作用。下面是一个最低限度的工作示例:
employee<- c('John','Peter','Mary',"Kate")
salarypre <- c(-1,23.5,33.2,34)
salarypost <- c(51,25.2,53,24.3)
startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14','2008-3-12'))
mydata<-data.frame(employee,salarypre,salarypost,startdate)
mydata.big<-gather(mydata,feature,val,salarypre,salarypost)
View(mydata.big)
但是,现在我无法使用“聚集”:
> mydata.big<-gather(mydata,feature,val,salarypre,salarypost)
Error: is.character(x) is not TRUE
>mydata.biglapply返回一个列表,用data.frame函数将其包装以进行转换,然后聚集应按预期工作:
mydata <- data.frame(lapply(mydata, function(x) ifelse(is.numeric(x) & x < 0, NA, x)))
gather(mydata, feature, val, salarypre, salarypost)
# employee startdate feature val
# 1 1 14914 salarypre NA
# 2 4 13963 salarypre 23.5
# 3 3 13586 salarypre 33.2
# 4 2 13950 salarypre 34.0
# 5 1 14914 salarypost 51.0
# 6 4 13963 salarypost 25.2
# 7 3 13586 salarypost 53.0
# 8 2 13950 salarypost 24.3
mydata%
聚集(特征、val、salarypre、salarypost)
mydata <- data.frame(lapply(mydata, function(x) ifelse(is.numeric(x) & x < 0, NA, x)))
gather(mydata, feature, val, salarypre, salarypost)
# employee startdate feature val
# 1 1 14914 salarypre NA
# 2 4 13963 salarypre 23.5
# 3 3 13586 salarypre 33.2
# 4 2 13950 salarypre 34.0
# 5 1 14914 salarypost 51.0
# 6 4 13963 salarypost 25.2
# 7 3 13586 salarypost 53.0
# 8 2 13950 salarypost 24.3
myFun <- function(x){ifelse(x < 0, NA, x)}
mydata %>% mutate_if(is.numeric, myFun) %>%
gather(feature, val, salarypre, salarypost)