简体   繁体   English

如何舍入R中具有一些非数字变量的数据框?

[英]How to round a data frame in R which have some non-numeric variables?

I got some decimals in 'mydata2'.我在“mydata2”中有一些小数。 So, I want to round only numeric variables in mydata2.所以,我只想在 mydata2 中舍入数字变量。 I have tried some methods like 'use purrr' or 'dplyr' etcs but I couldn't get it.我尝试了一些方法,如“使用 purrr”或“dplyr”等,但我无法理解。 How to I get it?我如何得到它?

   setwd("D:/데이터 사이언스/새 폴더") 
    mydata <- read.csv("Remote Learning.csv", header = TRUE) 
    mydata 
    
    mydata2 <- aggregate(학업.기여도~학교.유형 , mydata, mean)
    
    library(ggplot2) 
    
    ggplot(data=mydata2, aes(x = 학교.유형, y = 학업.기여도, fill = 학교.유형)) +geom_bar(stat = "identity", aes(color = 학교.유형)) 

This should work if the variables are numeric indeed (you have to rename your columns I suppose as there are some limitations for column names in R):如果变量确实是数字,这应该有效(我想你必须重命名你的列,因为 R 中的列名有一些限制):

library(dplyr)

df %>% 
  dplyr::select(col1 = 1, col2 = 2) %>% # enter all the columns you have and some simple names without special characters
  dplyr::mutate_if(is.numeric, ~ round(., 0))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM