I am having problems with this ifelse sentence inside map function:
df<-list(mtcars,mtcars) All I want to do is to organize each dataframe of this list this way:slice(x,c(n(),2:(n()-1),1))
map(df, ~ slice(.x,c(n(),2:(n()-1),1))) # it works well. But I cant do this inside ifelse when I set the condition x$cyl == 4:
map(df, ~ ifelse(sum(.x$cyl == 4) > 0, slice(.x,c(n(),2:(n()-1),1)), .x)) # The output is weird I tried to do the same using lapply but its not working:
lapply(df, function(x) ifelse(sum(x$cyl == 4) > 0, slice(x,c(n(),2:(n()-1),1)),x)) Any help?
https://stackoverflow.com/questions/67309125/how-to-use-ifelse-inside-map-function-in-r April 29, 2021 at 07:21AM
没有评论:
发表评论