簡體   English   中英

從python到R:嵌套列表

[英]From python to R: Nested lists

我有來自python的空間坐標數據,看起來像這樣:

[[179.0, -79.0], [127.0, 301.0], [-92.0, 310.0]]

我正在R中分析此數據,但我不知道如何獲取單個坐標,即如何獲取[179.0, -79.0] 我嘗試了不同的索引選項,但沒有運氣。 僅出於上下文考慮,我的數據框有一列帶有python列表列表的列。

任何幫助將非常感激。 謝謝!

澄清:

我想知道是否可以從'[[-188.0, -261.0], [-183.0, -30.0], [88.0, -26.0], [11.0, 339.0], [-198.0, 323.0], [-115.0, -8.0]] ' in R.

這就是R中的數據。我將一個csv文件加載到數據幀中,並且df$click_pos中的每個觀察值都具有類似'[[-188.0, -261.0], [-183.0, -30.0], [88.0, -26.0], [11.0, 339.0], [-198.0, 323.0], [-115.0, -8.0]]'

那么我可以從上面提取[-183.0, -30.0]嗎?

dfgood['click_pos'][[1]][1]給出

[1] [[-188.0, -261.0], [-183.0, -30.0], [88.0, -26.0], [11.0, 339.0], [-198.0, 323.0], [-115.0, -8.0]]
25 Levels: [[-102.0, 348.0], [-104.0, -37.0], [258.0, 87.0], [234.0, 418.0], [-93.0, 312.0], [118.0, -41.0]] ...   

請注意, typeof( dfgood['click_pos'][[1]][1])類型為int

我執行dfgood['click_pos'][[1]]時的R數據:

[1] [[-188.0, -261.0], [-183.0, -30.0], [88.0, -26.0], [11.0, 339.0], [-198.0, 323.0], [-115.0, -8.0]] 

[2] [[-12.0, 78.0], [4.0, -186.0], [170.0, -138.0], [45.0, 254.0], [-153.0, 232.0], [-140.0, -37.0], [-227.0, -28.0], [-62.0, -12.0]]  
.
.
.
[24] [[-14.0, 270.0], [-28.0, -164.0], [-302.0, -119.0], [-324.0, -278.0], [22.0, -259.0], [-316.0, -106.0]]                                 
25 Levels: [[-102.0, 348.0], [-104.0, -37.0], [258.0, 87.0], [234.0, 418.0], [-93.0, 312.0], [118.0, -41.0]] ...

dput(head(df))-修剪

structure(list(X = 0:5, subjID = c(2, 2, 2, 2, 2, 2), block = c(2, 
2, 2, 2, 2, 2), order_ntrials = c(1, 2, 3, 4, 5, 6), ntrials = c(13, 
4, 8, 16, 1, 21), routelen = c(5, 7, 4, 7, 6, 7), ideal_x = c(-183, 
4, -92, 19, -193, -133), ideal_y = c(-30, -186, 310, -26, 361, 
138), subj_x = c(-115, -62, -114, -107, -418, -199), subj_y = c(-8, 
-12, 323, 273, 305, 152), trial_rt = c(31.430577373947, 40.6340158929816, 
26.0440646470524, 50.8890219930327, 34.8192964689806, 37.9083978550043
), home_rt = c(5.36384105798788, 3.99772735999431, 3.22768635000102, 
5.48362414108124, 5.97196817304939, 5.93354129395448), all_pos = structure(c(8L, 
3L, 19L, 15L, 16L, 5L), .Label = c("[[-102.0, 348.0], [-101.0, 352.0], [-101.0, 352.0], [-101.0, 352.0], [-102.0, 352.0],...,[118.0, -41.0]]", 
"[[-11.0, 11.0], [-12.0, 14.0], [-12.0, 14.0],..., [6.0, 394.0]]", 
"[[-12.0, 78.0], [-12.0, 78.0], [-12.0, 78.0],..., [6.0, 394.0]]",
), class = "factor"), all_pos_times = structure(c(19L, 10L, 17L, 
6L, 12L, 9L), .Label = c("[15.970820963033475, 19.385453240014613, 19.89702452905476], ... "[2.4681653099833056, 5.662667237920687, 6.189063199912198]"
), class = "factor"), test_pos_idx = c(2, 2, 3, 5, 4, 6), mid_locs = structure(c(6L, 
24L, 15L, 3L, 18L, 4L), .Label = c("[[-100.0, 163.0], [54.0, 149.0], [7.0, -355.0], [-244.0, -363.0]]", 
"[[-104.0, -37.0], [258.0, 87.0], [234.0, 418.0]]", ...,"[[-122.0, 372.0], [-294.0, 372.0], [-235.0, -88.0], [19.0, -26.0], [-26.0, 277.0]]"), class = "factor"), test_pos = structure(c(4L, 
23L, 14L, 17L, 5L, 2L), .Label = c("[-100.0, 163.0]", "[-133.0, 138.0]", 
"[-150.0, -98.0]", "[-183.0, -30.0]", "[-193.0, 361.0]", "[-194.0, 213.0]", 
"[-302.0, -119.0]", "[-308.0, -254.0]", ... , "[80.0, -210.0]"), class = "factor"), 
    exclude = c(0, 0, 0, 0, 0, 0)), .Names = c("X", "subjID", 
"block", "order_ntrials", "ntrials", "routelen", "ideal_x", "ideal_y", 
"subj_x", "subj_y", "trial_rt", "home_rt", "all_pos", "all_pos_times", 
"click_pos", "click_pos_times", "test_pos_idx", "mid_locs", "test_pos", 
"exclude"), row.names = c(NA, 6L), class = "data.frame")

我在重新創建數據時遇到問題,因此我假設您來自python,數據看起來像這樣: [[179.0, -79.0], [127.0, 301.0], [-92.0, 310.0]] - [[179.0, -79.0], [127.0, 301.0], [-92.0, 310.0]] -python樣式。

使用R代碼重新創建它:

df <- list(list(179.0, -79.0), 
       list(127.0, 301.0),
       list(-92.0, 310.0))

您所擁有的是一個列表列表,如果您想使用索引來獲取項目,則需要使用[]

首先,在R最好使用str()函數來理解變量。

List of 3
 $ :List of 2
  ..$ : num 179
  ..$ : num -79
 $ :List of 2
  ..$ : num 127
  ..$ : num 301
 $ :List of 2
  ..$ : num -92
  ..$ : num 310

我們可以看到其中有3個列表。 接下來,我們可以開始使用[]和索引來獲取列表中的每個列表/項目(別忘了,與python不同, R使用1個索引):

如果輸入df[1]將進入我們的第一個列表:

[[1]]
[[1]][[1]]
[1] 179

[[1]][[2]]
[1] -79

但是,如果這樣做, df[[1]]將從我們的第一個列表中獲取第一項:

df[[1]]
[[1]]
[1] 179

[[2]]
[1] -79

如果我們使用第二個索引,例如df[[1]][1] ,我們將在列表中獲得第一項:

[[1]]
[1] 179

我認為這里這里的圖片非常有用: 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM