2017-06-24 17 views
1

複雑なリストをデータフレームに変換する方法を徹底的に調べましたが、私の場合はかなりユニークです:「リストにリストされたデータポイント」リストの大きなリストをデータフレームに変換する

問題: 私は観測値(データポイント)がさらにリストとしてリストされているいくつかの変数のリストを持つLargeリストを持っています。

以下は、複数のプライマリリストの代表的なリスト1の構造です。

:List of 5 ..$ type : chr "time" ..$ data :List of 134 .. ..$ : int 5624 .. ..$ : int 5625 .. ..$ : int 5627 .. .. [list output truncated] :List of 5 ..$ type : chr "dist" ..$ data :List of 134 .. ..$ : num 22321 .. ..$ : num 22313 .. .. [list output truncated] :List of 5 ..$ type : chr "cad" ..$ data :List of 134 .. ..$ : num 0.4 .. ..$ : num 0.6 .. .. [list output truncated]

この構造は、このようにリストの多数のために複製される - の目的は、すべてにわたって、「タイプ」によって定義されたすべての変数の「データ」部分を引っ張ることによって、データフレームにこのリストを変換することですメジャーリスト

注:他のメジャーリストには、サブリストの変数($ type)と観測数の多いものがあります。つまり、134のリストはLargeリストの他のリストには適用されません。

ありがとうございます。 さらに更新:以下

は、明確にするためにdput結果である:

`

list(structure(list(type = "time", data = list(2881L, 2885L, 
    2892L, 2893L, 2898L, 2899L, 2900L, 2901L, 2904L, 2907L, 2911L, 
    2912L, 2914L, 2918L), series_type = "distance", original_size = 14L, 
    resolution = "high"), .Names = c("type", "data", "series_type", 
"original_size", "resolution")), structure(list(type = "distance", 
    data = list(22512.4, 22548.3, 22605.5, 22615.1, 22670.3, 
     22692.2, 22705.8, 22719, 22752.3, 22771.7, 22815.6, 22827.9, 
     22851.6, 22892.5), series_type = "distance", original_size = 14L, 
    resolution = "high"), .Names = c("type", "data", "series_type", 
"original_size", "resolution")), structure(list(type = "grade_smooth", 
    data = list(-1.7, -3.1, -3.7, -3.2, -3, -3, -1.4, -2.5, -3.2, 
     -3.6, -3.7, -3.6, -2.7, -2.5)9.7, 10.3, 10.5, 10), 
    series_type = "distance", original_size = 14L, resolution = "high"), .Names = c("type", 
"data", "series_type", "original_size", "resolution")), structure(list(
    type = "time", data = list(665L, 668L, 671L, 674L, 677L, 
     680L, 683L, 686L, 689L, 692L, 695L, 698L, 701L, 704L), 
    series_type = "distance", original_size = 14L, resolution = "high"), .Names = c("type", 
"data", "series_type", "original_size", "resolution"))) 

` さらに更新:ソリューションの実装では、Iは、データフレーム等で終わる提供すると

これは:

Current Structure

Needed Structure

DFの問題:

STR(unlisting)

List of 11106 $ :List of 5 ..$ type : chr "time" ..$ data : int [1:180] 2426 2429 2432 2435 2438 2441 2445 2448 2451 2454 ... ..$ series_type : chr "distance" ..$ original_size: int 180 ..$ resolution : chr "high" $ :List of 5 ..$ type : chr "distance" ..$ data : num [1:180] 8802 8815 8826 8834 8844 ... ..$ series_type : chr "distance" ..$ original_size: int 180 ..$ resolution : chr "high" $ :List of 5 ..$ type : chr "grade_smooth" ..$ data : num [1:180] -1 -0.7 -0.6 -0.4 -0.2 -0.1 0 0.2 0.4 0.5 ... ..$ series_type : chr "distance" ..$ original_size: int 180 ..$ resolution : chr "high" $ :List of 5 ..$ type : chr "velocity_smooth" ..$ data : num [1:180] 2.7 3.9 3.9 3.2 3.1 3.6 3.6 4.4 4.5 3 ... ..$ series_type : chr "distance" ..$ original_size: int 180 ..$ resolution : chr "high" $ :List of 5 ..$ type : chr "time" ..$ data : int [1:74] 2999 3008 3009 3016 3020 3026 3027 3029 3030 3036 ... ..$ series_type : chr "distance" ..$ original_size: int 74 ..$ resolution : chr "high" $ :List of 5 ..$ type : chr "distance" ..$ data : num [1:74] 23661 23719 23735 23790 23825 ... ..$ series_type : chr "distance" ..$ original_size: int 74 ..$ resolution : chr "high" $ :List of 5 ..$ type : chr "grade_smooth" ..$ data : num [1:74] -1.3 -0.5 -0.5 0.6 0.7 1 1 0.5 1 1.9 ... ..$ series_type : chr "distance" ..$ original_size: int 74 ..$ resolution : chr "high" $ :List of 5 ..$ type : chr "velocity_smooth" ..$ data : num [1:74] 5.7 6.2 7.4 8.9 8.2 8.6 8.2 8.3 8.2 9.2 ... ..$ series_type : chr "distance" ..$ original_size: int 74 ..$ resolution : chr "high" $ :List of 5 ..$ type : chr "time" ..$ data : int [1:60] 396 403 410 416 418 424 429 437 447 455 ... ..$ series_type : chr "distance" ..$ original_size: int 60 ..$ resolution : chr "high" $ :List of 5 ..$ type : chr "distance" ..$ data : num [1:60] 935 964 992 1014 1020 ... ..$ series_type : chr "distance" ..$ original_size: int 60 ..$ resolution : chr "high" $ :List of 5 ..$ type : chr "heartrate" ..$ data : int [1:60] 121 117 117 111 108 107 109 112 116 121 ... ..$ series_type : chr "distance" ..$ original_size: int 60 ..$ resolution : chr "high" $ :List of 5 ..$ type : chr "grade_smooth" ..$ data : num [1:60] -0.7 -0.5 -0.3 0.1 0.4 0.8 1 0.9 1.4 1.6 ... ..$ series_type : chr "distance" ..$ original_size: int 60 ..$ resolution : chr "high" $ :List of 5 ..$ type : chr "velocity_smooth" ..$ data : num [1:60] 3 4 4.1 3.8 3.4 2.8 3.4 5.2 6.2 6.7 ... ..$ series_type : chr "distance" ..$ original_size: int 60 ..$ resolution : chr "high" [list output truncated]

+0

複雑なリストの例の部分を教えていただけますか?おそらく 'dput(mylist)'の結果を貼り付けるか、またはより小さなものを作ることによって? –

+0

ネストされたリストについて言えば、リスト構造のすべての繰り返しの詳細を実際に知らなくても解決策になるのは本当に難しいことです。 –

答えて

0

私はあなたのデータフレームに異なるが何であるかを知っていると私はよりを編集しますが、このコードの一部を実行しますヘルプ:

# fake data 
a <- list(type = "time", data = vector("list", 134), foo3 = vector("list", 134), foo4 = vector("list",134), foo5 = vector("list",134)) 
names(b) 
b <- list(type = "dist", data = vector("list", 134), foo3 = vector("list", 134), foo4 = vector("list",134), foo5 = vector("list",134)) 

c <- list(type = "cad", data = vector("list", 134), foo3 = vector("list", 134), foo4 = vector("list",134), foo5 = vector("list",134)) 
example <- list(a, b, c) 

# populate some fake data 
for(i in 1:length(example)){ 
    L <- length(example[[i]]) 
    for(j in 1:L){ 
    if(j == 1){ 
     example[[i]][[j]] <- c("time","dist","cad")[i] 
    } else { 
     example[[i]][[j]] <- rep(list(as.numeric(i+j)),134) 
    } 
    } 
} 

# unlist function on the right level of nested list 
unlisting <- lapply(example, function(L1) lapply(L1, unlist)) 
str(unlisting) 

listsofDFs <- lapply(unlisting, function(L1) as.data.frame(L1)) 
str(listsofDFs) 

listsofDFs 

EDIT:

df <- do.call(rbind,listsofDFs) 
df.split <- split(df$data, df$type) 
maxLength <- max(rapply(df.split, length)) 
df2.split <- vector("list", length(df.split)) 
z <- NULL 
for(i in 1:length(df.split)){ 
    z <- df.split[[i]] 
    length(z) <- maxLength 
    df2.split[[i]] <- z 
} 
df2 <- do.call(cbind.data.frame,df2.split) 
colnames(df2) <- names(df.split) 
copyrows <- match(df2$time, df$data) 
df2.final <- cbind.data.frame(df2,df[copyrows, -which(names(df) %in% c("type","data"))]) 
df2.final 

head(df2.final) 
    #time distance grade_smooth velocity_smooth heartrate series_type original_size resolution 
#1 2881 22512.4   -1.7    7.6  110 distance   14  high 
#2 2885 22548.3   -3.1    8.5  114 distance   14  high 
#3 2892 22605.5   -3.7    8.5  118 distance   14  high 
#4 2893 22615.1   -3.2    8.3  122 distance   14  high 
#5 2898 22670.3   -3.0   10.8  132 distance   14  high 
#6 2899 22692.2   -3.0   10.8  139 distance   14  high 
tail(df2.final) 
    #time distance grade_smooth velocity_smooth heartrate series_type original_size resolution 
#135 689  NA   NA    NA  NA distance   14  high 
#136 692  NA   NA    NA  NA distance   14  high 
#137 695  NA   NA    NA  NA distance   14  high 
#138 698  NA   NA    NA  NA distance   14  high 
#139 701  NA   NA    NA  NA distance   14  high 
#140 704  NA   NA    NA  NA distance   14  high 
+0

上記のコードをありがとう!それは構造をほぼシミュレートしますが、完全に同じではありません - サブリストの複数のリストを考慮していません - 得られた出力は多数のリストの1つで、潜在的に複数のdfsのリストを作成します。私が提供したサンプルの結果はあなたの理解に役立つでしょう - 多くのありがとう! –

+0

あなたのコードのさらなる実装では、rbindlist関数と組み合わせて、すべてを1つのdfに変換することができました!しかし、今問題は、すべての変数名が2番目の列の対応するデータで行内に表示されるようにデータがリストされていることです。これを転置して、変数xを持つすべての行に対してvar-xと対応するデータを持つ新しい列。 var-yとそれに対応するデータを新しい列に格納するなど... –

+0

do.call(rbind、listsofDFs)はすべてを1つのdfに入れます!私はちょっと混乱しているので、あなたが言っていることを読もうとしてください。 –

関連する問題