修复Mangled数据框

时间:2015-03-10 00:01:32

标签: mongodb dataframe rmongodb

我从这个数据框开始:

sample.df <- as.data.frame(list(a=rnorm(10),b=letters[1:10],c=1:10))

> str(sample.df)
'data.frame':   10 obs. of  3 variables:
 $ a: num  -0.0407 0.5403 -1.2885 2.2196 -1.3411 ...
 $ b: Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10
 $ c: int  1 2 3 4 5 6 7 8 9 10

我将它存储在mongoDB中。当我检索它(作为列表)时,它看起来像这样:

list(structure(list(`_id` = "54fd400dc37145c28f1077a0", `1` = structure(list(
    a = -0.0406532756571652, b = 1L, c = 1L), .Names = c("a", 
"b", "c")), `2` = structure(list(a = 0.540306074206446, b = 2L, 
    c = 2L), .Names = c("a", "b", "c")), `3` = structure(list(
    a = -1.28846428560021, b = 3L, c = 3L), .Names = c("a", "b", 
"c")), `4` = structure(list(a = 2.219612871209, b = 4L, c = 4L), .Names = c("a", 
"b", "c")), `5` = structure(list(a = -1.34109378068153, b = 5L, 
    c = 5L), .Names = c("a", "b", "c")), `6` = structure(list(
    a = -0.0971508202872865, b = 6L, c = 6L), .Names = c("a", 
"b", "c")), `7` = structure(list(a = -1.3419987318416, b = 7L, 
    c = 7L), .Names = c("a", "b", "c")), `8` = structure(list(
    a = -0.365034225520606, b = 8L, c = 8L), .Names = c("a", 
"b", "c")), `9` = structure(list(a = -0.0840790077727742, b = 9L, 
    c = 9L), .Names = c("a", "b", "c")), `10` = structure(list(
    a = 0.797477456457765, b = 10L, c = 10L), .Names = c("a", 
"b", "c"))), .Names = c("_id", "1", "2", "3", "4", "5", "6", 
"7", "8", "9", "10")))

我如何回到原始数据框? 我已经尝试删除_id成员并使用as.data.frame,但是我得到了这个:

l1 <- l[[1]]
l.df <- as.data.frame(l1[2:11])
> str(l.df)
'data.frame':   1 obs. of  30 variables:
 $ X1.a : num -0.0407
 $ X1.b : int 1
 $ X1.c : int 1
 $ X2.a : num 0.54
 $ X2.b : int 2
 $ X2.c : int 2
 $ X3.a : num -1.29
 $ X3.b : int 3
 $ X3.c : int 3
 $ X4.a : num 2.22
 $ X4.b : int 4
 $ X4.c : int 4
 $ X5.a : num -1.34
 $ X5.b : int 5
 $ X5.c : int 5
 $ X6.a : num -0.0972
 $ X6.b : int 6
 $ X6.c : int 6
 $ X7.a : num -1.34
 $ X7.b : int 7
 $ X7.c : int 7
 $ X8.a : num -0.365
 $ X8.b : int 8
 $ X8.c : int 8
 $ X9.a : num -0.0841
 $ X9.b : int 9
 $ X9.c : int 9
 $ X10.a: num 0.797
 $ X10.b: int 10
 $ X10.c: int 10

显然那不起作用......

1 个答案:

答案 0 :(得分:0)

这是我用thelatemail的帮助构建的:

mongolist.to.data.frame <- function(x){
  x.names <- names(x[[1]][2][[1]])
  x <- data.frame(setNames(lapply(x.names, function(y) sapply(x[[1]][-1], function(z) if(is.null(z[[y]])) return(NA) else return(z[[y]]))),x.names))
  return(x)
}