Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用dataframe进行聚集在lappy之后的行为不同_R_Dplyr_Lapply - Fatal编程技术网

使用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)