我有一个数据框列表:
> 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这是我的put(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"))
答案 0 :(得分: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 ...