简体   繁体   中英

R: Convert a factor of a data frame in a list to numeric

I have a list of data frames:

> 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 ...

I would like to convert the variable "IndicationStandard" in each data frame of the list to numeric. There was a similar question , but the solution converts all variables of the data frames to numeric. I only want the variable "IndicationStandard" to be numeric.

Does anybody have an idea how to do this?

@erasmortg here is my d 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"))

You can use this command. The problems are based on the strange column names starting with list$ . However, this solution keeps the column names.

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

The result:

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 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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