【问题标题】:R: Convert a factor of a data frame in a list to numericR:将列表中数据框的因子转换为数字
【发布时间】:2015-07-27 06:56:55
【问题描述】:

我有一个数据框列表:

> str(list6)
List of 2
 $ :'data.frame':   64 obs. of  2 variables:
  ..$ list$Stimulus          : Factor w/ 7 levels "108.wav","42.wav",..: 4 1 7 3 2 5 6 5 6 5 ...
  ..$ list$IndicationStandard: Factor w/ 2 levels "0","1": 2 2 1 1 1 2 2 2 1 2 ...
 $ :'data.frame':   64 obs. of  2 variables:
  ..$ list$Stimulus          : Factor w/ 7 levels "108.wav","42.wav",..: 4 1 7 3 2 5 6 5 6 5 ...
  ..$ list$IndicationStandard: Factor w/ 2 levels "0","1": 1 1 1 1 1 2 2 2 1 2 ...

我想将列表的每个数据框中的变量“IndicationStandard”转换为数字。 有一个similar question,但该解决方案将数据帧的所有变量转换为数字。我只希望变量“IndicationStandard”为数字。

有人知道怎么做吗?

@erasmortg 这是我的 dput(list6)

list(structure(list(`list$Stimulus` = structure(c(4L, 1L, 7L, 
3L, 2L, 5L, 6L, 5L, 6L, 5L, 2L, 4L, 5L, 3L, 1L, 7L, 5L, 2L, 1L, 
4L, 6L, 3L, 7L, 5L, 5L, 3L, 1L, 2L, 5L, 4L, 7L, 6L, 5L, 2L, 5L, 
1L, 4L, 3L, 6L, 7L, 4L, 7L, 1L, 6L, 5L, 5L, 3L, 2L, 7L, 5L, 3L, 
5L, 6L, 1L, 2L, 4L, 1L, 2L, 4L, 5L, 6L, 7L, 5L, 3L), .Label = c("108.wav", 
"42.wav", "53.wav", "64.wav", "75.wav", "86.wav", "97.wav"), class = "factor"), 
    `list$IndicationStandard` = structure(c(2L, 2L, 1L, 1L, 1L, 
    2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 
    2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 
    1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L), .Label = c("0", 
    "1"), class = "factor")), .Names = c("list$Stimulus", "list$IndicationStandard"
), row.names = c(NA, -64L), class = "data.frame"), structure(list(
    `list$Stimulus` = structure(c(4L, 1L, 7L, 3L, 2L, 5L, 6L, 
    5L, 6L, 5L, 2L, 4L, 5L, 3L, 1L, 7L, 5L, 2L, 1L, 4L, 6L, 3L, 
    7L, 5L, 5L, 3L, 1L, 2L, 5L, 4L, 7L, 6L, 5L, 2L, 5L, 1L, 4L, 
    3L, 6L, 7L, 4L, 7L, 1L, 6L, 5L, 5L, 3L, 2L, 7L, 5L, 3L, 5L, 
    6L, 1L, 2L, 4L, 1L, 2L, 4L, 5L, 6L, 7L, 5L, 3L), .Label = c("108.wav", 
    "42.wav", "53.wav", "64.wav", "75.wav", "86.wav", "97.wav"
    ), class = "factor"), `list$IndicationStandard` = structure(c(1L, 
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 
    1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 
    1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 1L), .Label = c("0", "1"), class = "factor")), .Names = c("list$Stimulus", 
"list$IndicationStandard"), row.names = c(NA, -64L), class = "data.frame"))

【问题讨论】:

  • dput 输出给了我错误。你能检查一下吗
  • @akrun 我刚刚输入了 dput(list6) 这就是我得到的。我错过了什么吗?
  • 您的更新仍然给我错误。只需将该输出复制并粘贴到您的控制台中即可。看看会不会出错
  • 我复制粘贴它并没有给我错误。 @Sven Hohenstein 的解决方案对我有用。
  • 我的解决方案现在有效。更新了它。因为列名是list$IndicationStandard

标签: r dataframe


【解决方案1】:

您可以使用此命令。问题基于以list$ 开头的奇怪列名。但是,此解决方案保留了列名。

lapply(list6, within, 
     "list$IndicationStandard" <- as.numeric(as.character(get("list$IndicationStandard"))))

结果:

List of 2
 $ :'data.frame':   64 obs. of  2 variables:
  ..$ list$Stimulus          : Factor w/ 7 levels "108.wav","42.wav",..: 4 1 7 3 2 5 6 5 6 5 ...
  ..$ list$IndicationStandard: num [1:64] 1 1 0 0 0 1 1 1 0 1 ...
 $ :'data.frame':   64 obs. of  2 variables:
  ..$ list$Stimulus          : Factor w/ 7 levels "108.wav","42.wav",..: 4 1 7 3 2 5 6 5 6 5 ...
  ..$ list$IndicationStandard: num [1:64] 0 0 0 0 0 1 1 1 0 1 ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-24
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 2019-04-16
    相关资源
    最近更新 更多