损坏的data.frame包含列表 - 如何转换为列?

时间:2012-01-19 09:34:15

标签: r list dataframe

我有data.frame,名为“so_data”。

第13:23列是list个,它包含空单元格和单个值。如何将它们转换为数字向量?

我试过了:

matrix(unlist(rang[13:23]), ncol=11, byrow=F)

但显然我松开了所有空洞。这让一切都搞砸了。

as.data.frame(do.call(cbind, rang)))

基本上不会更改数据。

以下是数据:

so_data <- structure(list(id = c(83L, 84L, 85L, 86L, 88L, 89L, 90L, 91L, 
92L, 93L, 94L, 95L, 97L, 98L, 99L, 100L, 101L, 102L, 104L, 105L
), motivation..1. = structure(c(7L, 3L, 5L, 5L, 5L, 10L, 5L, 
5L, 5L, 5L, 2L, 12L, 5L, 2L, 8L, 5L, 5L, 9L, 3L, 5L), .Label = c("", 
"Kald", "Udviklingspotentiale", "Alsidigt arbejdsliv", "Noget jeg kan lide", 
"egen", "godti", "indko", "inter", "jobsi", "samfn", "statu"), class = "factor"), 
    motivation..2. = structure(c(5L, 11L, 9L, 4L, 3L, 9L, 4L, 
    4L, 7L, 4L, 5L, 3L, 7L, 5L, 7L, 10L, 7L, 6L, 5L, 7L), .Label = c("", 
    "Kald", "Udviklingspotentiale", "Alsidigt arbejdsliv", "Noget jeg kan lide", 
    "egen", "godti", "indko", "inter", "jobsi", "samfn", "statu"
    ), class = "factor"), motivation..3. = structure(c(4L, 9L, 
    11L, 7L, 4L, 6L, 3L, 7L, 9L, 9L, 7L, 2L, 4L, 9L, 10L, 8L, 
    9L, 2L, 4L, 3L), .Label = c("", "Kald", "Udviklingspotentiale", 
    "Alsidigt arbejdsliv", "Noget jeg kan lide", "egen", "godti", 
    "indko", "inter", "jobsi", "samfn", "statu"), class = "factor"), 
    motivation..4. = structure(c(11L, 5L, 2L, 10L, 9L, 3L, 8L, 
    11L, 2L, 10L, 10L, 7L, 3L, 7L, 5L, 9L, 3L, 5L, 2L, 8L), .Label = c("", 
    "Kald", "Udviklingspotentiale", "Alsidigt arbejdsliv", "Noget jeg kan lide", 
    "egen", "godti", "indko", "inter", "jobsi", "samfn", "statu"
    ), class = "factor"), motivation..5. = structure(c(3L, 7L, 
    10L, 3L, 11L, 5L, 11L, 3L, 4L, 8L, 9L, 5L, 8L, 11L, 3L, 3L, 
    4L, 7L, 6L, 10L), .Label = c("", "Kald", "Udviklingspotentiale", 
    "Alsidigt arbejdsliv", "Noget jeg kan lide", "egen", "godti", 
    "indko", "inter", "jobsi", "samfn", "statu"), class = "factor"), 
    motivation..6. = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), motivation..7. = c(NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA), motivation..8. = c(NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
    motivation..9. = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), motivation..10. = c(NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA), motivation..11. = c(NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
    ), godti = structure(list(`3` = structure(2L, .Names = "motivation..1."), 
        `4` = structure(6L, .Names = "motivation..5."), `5` = structure(integer(0), .Names = character(0)), 
        `6` = structure(4L, .Names = "motivation..3."), `8` = structure(integer(0), .Names = character(0)), 
        `9` = structure(integer(0), .Names = character(0)), `10` = structure(integer(0), .Names = character(0)), 
        `11` = structure(4L, .Names = "motivation..3."), `12` = structure(3L, .Names = "motivation..2."), 
        `13` = structure(integer(0), .Names = character(0)), 
        `14` = structure(4L, .Names = "motivation..3."), `15` = structure(5L, .Names = "motivation..4."), 
        `17` = structure(3L, .Names = "motivation..2."), `18` = structure(5L, .Names = "motivation..4."), 
        `19` = structure(3L, .Names = "motivation..2."), `20` = structure(integer(0), .Names = character(0)), 
        `21` = structure(3L, .Names = "motivation..2."), `22` = structure(6L, .Names = "motivation..5."), 
        `24` = structure(integer(0), .Names = character(0)), 
        `25` = structure(3L, .Names = "motivation..2.")), .Names = c("3", 
    "4", "5", "6", "8", "9", "10", "11", "12", "13", "14", "15", 
    "17", "18", "19", "20", "21", "22", "24", "25")), Udviklingspotentiale = structure(list(
        `3` = structure(6L, .Names = "motivation..5."), `4` = structure(2L, .Names = "motivation..1."), 
        `5` = structure(integer(0), .Names = character(0)), `6` = structure(6L, .Names = "motivation..5."), 
        `8` = structure(3L, .Names = "motivation..2."), `9` = structure(5L, .Names = "motivation..4."), 
        `10` = structure(4L, .Names = "motivation..3."), `11` = structure(6L, .Names = "motivation..5."), 
        `12` = structure(integer(0), .Names = character(0)), 
        `13` = structure(integer(0), .Names = character(0)), 
        `14` = structure(integer(0), .Names = character(0)), 
        `15` = structure(3L, .Names = "motivation..2."), `17` = structure(5L, .Names = "motivation..4."), 
        `18` = structure(integer(0), .Names = character(0)), 
        `19` = structure(6L, .Names = "motivation..5."), `20` = structure(6L, .Names = "motivation..5."), 
        `21` = structure(5L, .Names = "motivation..4."), `22` = structure(integer(0), .Names = character(0)), 
        `24` = structure(2L, .Names = "motivation..1."), `25` = structure(4L, .Names = "motivation..3.")), .Names = c("3", 
    "4", "5", "6", "8", "9", "10", "11", "12", "13", "14", "15", 
    "17", "18", "19", "20", "21", "22", "24", "25")), `Noget jeg kan lide` = structure(list(
        `3` = structure(3L, .Names = "motivation..2."), `4` = structure(5L, .Names = "motivation..4."), 
        `5` = structure(2L, .Names = "motivation..1."), `6` = structure(2L, .Names = "motivation..1."), 
        `8` = structure(2L, .Names = "motivation..1."), `9` = structure(6L, .Names = "motivation..5."), 
        `10` = structure(2L, .Names = "motivation..1."), `11` = structure(2L, .Names = "motivation..1."), 
        `12` = structure(2L, .Names = "motivation..1."), `13` = structure(2L, .Names = "motivation..1."), 
        `14` = structure(3L, .Names = "motivation..2."), `15` = structure(6L, .Names = "motivation..5."), 
        `17` = structure(2L, .Names = "motivation..1."), `18` = structure(3L, .Names = "motivation..2."), 
        `19` = structure(5L, .Names = "motivation..4."), `20` = structure(2L, .Names = "motivation..1."), 
        `21` = structure(2L, .Names = "motivation..1."), `22` = structure(5L, .Names = "motivation..4."), 
        `24` = structure(3L, .Names = "motivation..2."), `25` = structure(2L, .Names = "motivation..1.")), .Names = c("3", 
    "4", "5", "6", "8", "9", "10", "11", "12", "13", "14", "15", 
    "17", "18", "19", "20", "21", "22", "24", "25")), jobsi = structure(list(
        `3` = structure(integer(0), .Names = character(0)), `4` = structure(integer(0), .Names = character(0)), 
        `5` = structure(6L, .Names = "motivation..5."), `6` = structure(5L, .Names = "motivation..4."), 
        `8` = structure(integer(0), .Names = character(0)), `9` = structure(2L, .Names = "motivation..1."), 
        `10` = structure(integer(0), .Names = character(0)), 
        `11` = structure(integer(0), .Names = character(0)), 
        `12` = structure(integer(0), .Names = character(0)), 
        `13` = structure(5L, .Names = "motivation..4."), `14` = structure(5L, .Names = "motivation..4."), 
        `15` = structure(integer(0), .Names = character(0)), 
        `17` = structure(integer(0), .Names = character(0)), 
        `18` = structure(integer(0), .Names = character(0)), 
        `19` = structure(4L, .Names = "motivation..3."), `20` = structure(3L, .Names = "motivation..2."), 
        `21` = structure(integer(0), .Names = character(0)), 
        `22` = structure(integer(0), .Names = character(0)), 
        `24` = structure(integer(0), .Names = character(0)), 
        `25` = structure(6L, .Names = "motivation..5.")), .Names = c("3", 
    "4", "5", "6", "8", "9", "10", "11", "12", "13", "14", "15", 
    "17", "18", "19", "20", "21", "22", "24", "25")), Kald = structure(list(
        `3` = structure(integer(0), .Names = character(0)), `4` = structure(integer(0), .Names = character(0)), 
        `5` = structure(5L, .Names = "motivation..4."), `6` = structure(integer(0), .Names = character(0)), 
        `8` = structure(integer(0), .Names = character(0)), `9` = structure(integer(0), .Names = character(0)), 
        `10` = structure(integer(0), .Names = character(0)), 
        `11` = structure(integer(0), .Names = character(0)), 
        `12` = structure(5L, .Names = "motivation..4."), `13` = structure(integer(0), .Names = character(0)), 
        `14` = structure(2L, .Names = "motivation..1."), `15` = structure(4L, .Names = "motivation..3."), 
        `17` = structure(integer(0), .Names = character(0)), 
        `18` = structure(2L, .Names = "motivation..1."), `19` = structure(integer(0), .Names = character(0)), 
        `20` = structure(integer(0), .Names = character(0)), 
        `21` = structure(integer(0), .Names = character(0)), 
        `22` = structure(4L, .Names = "motivation..3."), `24` = structure(5L, .Names = "motivation..4."), 
        `25` = structure(integer(0), .Names = character(0))), .Names = c("3", 
    "4", "5", "6", "8", "9", "10", "11", "12", "13", "14", "15", 
    "17", "18", "19", "20", "21", "22", "24", "25")), statu = structure(list(
        `3` = structure(integer(0), .Names = character(0)), `4` = structure(integer(0), .Names = character(0)), 
        `5` = structure(integer(0), .Names = character(0)), `6` = structure(integer(0), .Names = character(0)), 
        `8` = structure(integer(0), .Names = character(0)), `9` = structure(integer(0), .Names = character(0)), 
        `10` = structure(integer(0), .Names = character(0)), 
        `11` = structure(integer(0), .Names = character(0)), 
        `12` = structure(integer(0), .Names = character(0)), 
        `13` = structure(integer(0), .Names = character(0)), 
        `14` = structure(integer(0), .Names = character(0)), 
        `15` = structure(2L, .Names = "motivation..1."), `17` = structure(integer(0), .Names = character(0)), 
        `18` = structure(integer(0), .Names = character(0)), 
        `19` = structure(integer(0), .Names = character(0)), 
        `20` = structure(integer(0), .Names = character(0)), 
        `21` = structure(integer(0), .Names = character(0)), 
        `22` = structure(integer(0), .Names = character(0)), 
        `24` = structure(integer(0), .Names = character(0)), 
        `25` = structure(integer(0), .Names = character(0))), .Names = c("3", 
    "4", "5", "6", "8", "9", "10", "11", "12", "13", "14", "15", 
    "17", "18", "19", "20", "21", "22", "24", "25")), indko = structure(list(
        `3` = structure(integer(0), .Names = character(0)), `4` = structure(integer(0), .Names = character(0)), 
        `5` = structure(integer(0), .Names = character(0)), `6` = structure(integer(0), .Names = character(0)), 
        `8` = structure(integer(0), .Names = character(0)), `9` = structure(integer(0), .Names = character(0)), 
        `10` = structure(5L, .Names = "motivation..4."), `11` = structure(integer(0), .Names = character(0)), 
        `12` = structure(integer(0), .Names = character(0)), 
        `13` = structure(6L, .Names = "motivation..5."), `14` = structure(integer(0), .Names = character(0)), 
        `15` = structure(integer(0), .Names = character(0)), 
        `17` = structure(6L, .Names = "motivation..5."), `18` = structure(integer(0), .Names = character(0)), 
        `19` = structure(2L, .Names = "motivation..1."), `20` = structure(4L, .Names = "motivation..3."), 
        `21` = structure(integer(0), .Names = character(0)), 
        `22` = structure(integer(0), .Names = character(0)), 
        `24` = structure(integer(0), .Names = character(0)), 
        `25` = structure(5L, .Names = "motivation..4.")), .Names = c("3", 
    "4", "5", "6", "8", "9", "10", "11", "12", "13", "14", "15", 
    "17", "18", "19", "20", "21", "22", "24", "25")), inter = structure(list(
        `3` = structure(integer(0), .Names = character(0)), `4` = structure(4L, .Names = "motivation..3."), 
        `5` = structure(3L, .Names = "motivation..2."), `6` = structure(integer(0), .Names = character(0)), 
        `8` = structure(5L, .Names = "motivation..4."), `9` = structure(3L, .Names = "motivation..2."), 
        `10` = structure(integer(0), .Names = character(0)), 
        `11` = structure(integer(0), .Names = character(0)), 
        `12` = structure(4L, .Names = "motivation..3."), `13` = structure(4L, .Names = "motivation..3."), 
        `14` = structure(6L, .Names = "motivation..5."), `15` = structure(integer(0), .Names = character(0)), 
        `17` = structure(integer(0), .Names = character(0)), 
        `18` = structure(4L, .Names = "motivation..3."), `19` = structure(integer(0), .Names = character(0)), 
        `20` = structure(5L, .Names = "motivation..4."), `21` = structure(4L, .Names = "motivation..3."), 
        `22` = structure(2L, .Names = "motivation..1."), `24` = structure(integer(0), .Names = character(0)), 
        `25` = structure(integer(0), .Names = character(0))), .Names = c("3", 
    "4", "5", "6", "8", "9", "10", "11", "12", "13", "14", "15", 
    "17", "18", "19", "20", "21", "22", "24", "25")), `Alsidigt arbejdsliv` = structure(list(
        `3` = structure(4L, .Names = "motivation..3."), `4` = structure(integer(0), .Names = character(0)), 
        `5` = structure(integer(0), .Names = character(0)), `6` = structure(3L, .Names = "motivation..2."), 
        `8` = structure(4L, .Names = "motivation..3."), `9` = structure(integer(0), .Names = character(0)), 
        `10` = structure(3L, .Names = "motivation..2."), `11` = structure(3L, .Names = "motivation..2."), 
        `12` = structure(6L, .Names = "motivation..5."), `13` = structure(3L, .Names = "motivation..2."), 
        `14` = structure(integer(0), .Names = character(0)), 
        `15` = structure(integer(0), .Names = character(0)), 
        `17` = structure(4L, .Names = "motivation..3."), `18` = structure(integer(0), .Names = character(0)), 
        `19` = structure(integer(0), .Names = character(0)), 
        `20` = structure(integer(0), .Names = character(0)), 
        `21` = structure(6L, .Names = "motivation..5."), `22` = structure(integer(0), .Names = character(0)), 
        `24` = structure(4L, .Names = "motivation..3."), `25` = structure(integer(0), .Names = character(0))), .Names = c("3", 
    "4", "5", "6", "8", "9", "10", "11", "12", "13", "14", "15", 
    "17", "18", "19", "20", "21", "22", "24", "25")), samfn = structure(list(
        `3` = structure(5L, .Names = "motivation..4."), `4` = structure(3L, .Names = "motivation..2."), 
        `5` = structure(4L, .Names = "motivation..3."), `6` = structure(integer(0), .Names = character(0)), 
        `8` = structure(6L, .Names = "motivation..5."), `9` = structure(integer(0), .Names = character(0)), 
        `10` = structure(6L, .Names = "motivation..5."), `11` = structure(5L, .Names = "motivation..4."), 
        `12` = structure(integer(0), .Names = character(0)), 
        `13` = structure(integer(0), .Names = character(0)), 
        `14` = structure(integer(0), .Names = character(0)), 
        `15` = structure(integer(0), .Names = character(0)), 
        `17` = structure(integer(0), .Names = character(0)), 
        `18` = structure(6L, .Names = "motivation..5."), `19` = structure(integer(0), .Names = character(0)), 
        `20` = structure(integer(0), .Names = character(0)), 
        `21` = structure(integer(0), .Names = character(0)), 
        `22` = structure(integer(0), .Names = character(0)), 
        `24` = structure(integer(0), .Names = character(0)), 
        `25` = structure(integer(0), .Names = character(0))), .Names = c("3", 
    "4", "5", "6", "8", "9", "10", "11", "12", "13", "14", "15", 
    "17", "18", "19", "20", "21", "22", "24", "25")), egen = structure(list(
        `3` = structure(integer(0), .Names = character(0)), `4` = structure(integer(0), .Names = character(0)), 
        `5` = structure(integer(0), .Names = character(0)), `6` = structure(integer(0), .Names = character(0)), 
        `8` = structure(integer(0), .Names = character(0)), `9` = structure(4L, .Names = "motivation..3."), 
        `10` = structure(integer(0), .Names = character(0)), 
        `11` = structure(integer(0), .Names = character(0)), 
        `12` = structure(integer(0), .Names = character(0)), 
        `13` = structure(integer(0), .Names = character(0)), 
        `14` = structure(integer(0), .Names = character(0)), 
        `15` = structure(integer(0), .Names = character(0)), 
        `17` = structure(integer(0), .Names = character(0)), 
        `18` = structure(integer(0), .Names = character(0)), 
        `19` = structure(integer(0), .Names = character(0)), 
        `20` = structure(integer(0), .Names = character(0)), 
        `21` = structure(integer(0), .Names = character(0)), 
        `22` = structure(3L, .Names = "motivation..2."), `24` = structure(6L, .Names = "motivation..5."), 
        `25` = structure(integer(0), .Names = character(0))), .Names = c("3", 
    "4", "5", "6", "8", "9", "10", "11", "12", "13", "14", "15", 
    "17", "18", "19", "20", "21", "22", "24", "25"))), .Names = c("id", 
"motivation..1.", "motivation..2.", "motivation..3.", "motivation..4.", 
"motivation..5.", "motivation..6.", "motivation..7.", "motivation..8.", 
"motivation..9.", "motivation..10.", "motivation..11.", "godti", 
"Udviklingspotentiale", "Noget jeg kan lide", "jobsi", "Kald", 
"statu", "indko", "inter", "Alsidigt arbejdsliv", "samfn", "egen"
), row.names = c(3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 
14L, 15L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L), class = "data.frame")

编辑: 是什么造成了这个烂摊子?

上面的data.frame以12列id和11个因素开始。这些是so_data[1:12]

调查中的排名问题产生了11个因素。像这样的东西“对于选择大学的以下11个理由中的每一个,请排列五个原因,对您来说最重要”。

然后,11个列中的每一个代表一个选择。由于只允许五个选项,因此只有五个第一列(so_data[2:6])包含相关数据。

现在 - 对于我做一些计算和绘图,我需要稍微改变一下数据。而不是有5列代表“优先级1”,“优先级2”等,我想要11列代表一个原因。然后,每列都根据受访者为此分配的优先级来确定值。

如,

而不是:

-------------|Priority 1------- | Priority 2---| ... | Priority 5 |
respondent1  | raeason X        | Reason Y     | ... | reason z    | 
...

我想:

-------------| Reason 1 -------  | Reason 2    ---| ... | Reason 11 |
respondent1  | Priority z        | Priority 2     | ... | Priority 5
...

这是一个聪明人帮助我的代码:

startcolidx = 2
endcolidx = 6
factors = unique(unlist(rang[,startcolidx:endcolidx]))


for(f in as.character(factors)) {
   rang[[f]] = apply(rang, 1, function(arow) which(arow == f))
}

1 个答案:

答案 0 :(得分:3)

您的data.frame显然已损坏:某些列是1-或0元素向量的列表 - 这不应该发生在data.frame中。 而不是试图修复它,修复生成它的代码可能更明智,更容易。

如果您坚持修复数据而不是根本问题,则可能会有以下情况:

do.call(data.frame, 
  lapply( so_data, 
    function (u) unlist( 
      if( is.list(u) ) lapply(u, function(v) c(v,NA)[1]) 
      else u 
)))

修改:您可以使用meltdcast转换初始数据。

library(reshape2)
d <- so_data[1:6]

# Transform the data to a tall format:
# it is much easier to process data in tall format.
d <- melt(d, id.vars="id" )

# Transfrom the data to a wide format: 
# just specify the rows and the columns on each side of ~.
# It is rarely necessary to specify value.var.
d <- dcast( d, id ~ value, value.var="variable" )