【问题标题】:Converting factor to integer [duplicate]将因子转换为整数[重复]
【发布时间】:2019-12-01 15:52:17
【问题描述】:

使用 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, : “sum”对因子没有意义

发生。我知道这是因为它没有被读取为整数,但我不知道如何转换。

谢谢

来自 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")

【问题讨论】:

  • RENA 中的哪一列是用as.integer 转换为数字的因子?看看一些有代表性的数据会很有用,你能编辑你的问题并添加dput(head(RENA))的输出吗?
  • 我已经添加了 dput 信息
  • 谢谢,手动删除了#DIV/0。下次我会知道要注意什么。

标签: r


【解决方案1】:

只需修改您的第一行(read.csv)以在导入期间指定每个变量的性质。

【讨论】:

  • 我该怎么做?
  • @Megascops 检查文档中的 read.csv 使用“colclasses”参数为列假定的类向量。可能的值是 NA(默认值,当使用 type.convert 时)、“NULL”(当列被跳过时)、原子向量类之一(逻辑、整数、数字、复杂、字符、原始)或“因子"、"日期" 或 "POSIXct"。否则需要一个 as 方法(来自包方法)用于从“字符”转换为指定的正式类。请注意,colClasses 是按列(而不是每个变量)指定的,因此包括行名列(如果有)。
猜你喜欢
  • 2015-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
相关资源
最近更新 更多