谓词函数,也叫判断函数(predicate function) mutate_if()转换数据类型,挺方便的 # 将所有因子列转化成字符my_data%>%mutate_if(is.factor,as.character)# 将左右数字列四舍五入my_data%>%mutate_if(is.numeric,round,digits=0) mutate_if()应用于多个转换(Multiple transformations) 此时需要通过函数的list来传...
错误1:"Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "NULL"" 解决方法:这个错误通常表示您没有正确加载dplyr包。请确保使用library(dplyr)或require(dplyr)命令加载dplyr包。 错误2:"Error: object 'xxx' not found" 解决方法:这个错误通常...
mutate(across(!name, as.factor)) # 保留或删除列,默认保留所有列 df <- tibble(x = 1, y = 2, a = "a", b = "b") df %>% mutate(z = x + y, .keep = "all") # the default df %>% mutate(z = x + y, .keep = "used") df %>% mutate(z = x + y, .keep = "unus...
mutate(across(mpg:drat, as.factor)) %>% group_by(cyl) %>% summarise(across(where(is.numeric), mean)) ## # A tibble: 3 x 7 ## cyl wt qsec vs am gear carb ## * <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 4 2.29 19.1 0.909 0.727 4.09 1.55 ## 2 6 3.12 18.0...
# change `id` to factor (otherwise it would display as a continuous variable on the plot) mutate(id = as.factor(id)) chelonia_trends <- ggplot(marine_final, aes(x = year, y = pop, colour = location)) + geom_point(size = 2, alpha = 0.7) + ...
mutate_if()对于将变量从一种类型转换为另一种类型特别有用。...# 将所有因子列转化成字符 my_data %>% mutate_if(is.factor, as.character) # 将左右数字列四舍五入 my_data %>% mutate_if(is.numeric 4.1K20 「R」dplyr 列式计算 across() 的基本用法开始,特别是将其应用于 summarise() 中和展示...
Dplyr(https://dplyr.tidyverse.org/)是一种数据操作语法,提供了一组一致的动词,帮助我们解决最常见的数据操作,比如行操作(filter、slice、arrange)、列操作(slelect、rename、mutate、relocate)、折叠操作(summarise)、合并table(left_join、right_join、inner_join)。查看包中的所有函数: ...
先得处理缺失值summ21<-summarise_if(df,is.numeric,funs(n(),mean,median))#或者num_data<-df[sapply(df,is.numeric)]summ21_2<-summarise_all(num_data,funs(n(),mean,median)) 22.总结因子变量 df22<-df11%>%mutate(Gender=as.factor(Gender))summ22<-summarise_all(df22["Gender"],funs(...
I want to change the levels of a factor in a data frame, using mutate. Simple example: library("dplyr") dat <- data.frame(x = factor("A"), y = 1) mutate(dat,levels(x) = "B") I get: Error: Unexpected '=' in "mutate(dat,levels(x) =" Why is this ...
(0,0,3,7,0,0,0,3,4,0,0,3,6,8,0,0,0,4,7,0))df<-df%>%mutate(Year=as.factor(Year),Tree=as.factor(Tree),DOY=as.numeric(DOY),Enlarging=as.numeric(Enlarging),Maturing=as.numeric(Maturing))print(df)Year Tree DOY Enlarging Maturing...