将因子转换为整数

时间:2019-07-23 14:53:06

标签: r

使用RStudio将.csv中的因子转换为整数。

嗨,我知道这个问题经常被问到,但是我一直在努力将事情缠着一个小时没有成功。

在我的.csv文件中,“ Weighted.average”是对Weighted.count / count(在转换之前)的计算,但是当我在R中使用该文件时,尽管它是完全数字的(带小数点),但它还是一个因素。

我的目标是使用Weighted.average的数值汇总数据。但是由于仍然被认为是一个因素,因此无法正常工作。我对R不熟悉,因此在将其他示例转换为我自己的示例时遇到了麻烦。

谢谢

RENA <- read.csv('RENA.csv')
RENAVG  <-     aggregate(Weighted.average~Diet+DGRP.Line, data = RENA, FUN = sum) 
ggplot(RENAVG, aes(x=DGRP.Line, y=Weighted.average, colour=Diet)) +
  geom_point()

预计将使用Weighted.average形成点图,错误

  

Summary.factor(c(3L,4L,1L,1L,1L,1L,1L,1L,1L,1L,1L,:         “总和”对因素没有意义

发生。我知道是由于未将其读取为整数,但我对如何转换一无所知。

谢谢

dput的输出

> dput(head(RENA))
structure(list(DGRP.Line = structure(c(19L, 19L, 19L, 19L, 20L, 
20L), .Label = c("105a", "105b", "348", "354", "362a", "362b", 
"391a", "391b", "392", "397", "405", "486a", "486b", "712", "721", 
"737", "757a", "757b", "853", "879"), class = "factor"), Diet = structure(c(1L, 
1L, 2L, 2L, 1L, 1L), .Label = c("Control", "Rena"), class = "factor"), 
    Sex = structure(c(2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Female", 
    "Male"), class = "factor"), Count = c(0L, 0L, 0L, 0L, 1L, 
    0L), Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("16/07/2019", 
    "17/07/2019", "18/07/2019", "19/07/2019", "20/07/2019", "21/07/2019", 
    "22/07/2019"), class = "factor"), Day = c(1L, 1L, 1L, 1L, 
    1L, 1L), Weighted.count = c(0L, 0L, 0L, 0L, 1L, 0L), Weighted.average = structure(c(60L, 
    59L, 52L, 63L, 44L, 36L), .Label = c("", "#DIV/0!", "1.8", 
    "1.818181818", "2", "2.275862069", "2.282608696", "2.478873239", 
    "2.635135135", "2.705882353", "2.824561404", "2.903614458", 
    "2.911392405", "2.917525773", "3", "3.034090909", "3.038461538", 
    "3.083333333", "3.119402985", "3.125", "3.154929577", "3.175438596", 
    "3.1875", "3.220338983", "3.254237288", "3.263157895", "3.314606742", 
    "3.341463415", "3.35", "3.435483871", "3.5", "3.6", "3.606557377", 
    "3.666666667", "3.6875", "3.694214876", "3.797619048", "3.813953488", 
    "3.833333333", "3.875", "3.909090909", "3.916666667", "4.045454545", 
    "4.047169811", "4.111111111", "4.333333333", "4.40625", "4.444444444", 
    "4.529411765", "4.617021277", "4.620689655", "4.666666667", 
    "4.714285714", "4.732283465", "4.821428571", "4.823529412", 
    "4.846153846", "4.851851852", "4.855263158", "4.884615385", 
    "4.956521739", "5", "5.115384615", "5.230769231", "5.343283582", 
    "5.45", "5.464285714", "5.484848485", "5.538461538", "5.551724138", 
    "5.970588235", "6", "6.2"), class = "factor")), row.names = c(NA, 
6L), class = "data.frame")

1 个答案:

答案 0 :(得分:-1)

只需修改第一行(read.csv)以指定导入过程中每个变量的性质。