简体   繁体   English

从python到R:嵌套列表

[英]From python to R: Nested lists

I have spatial coordinate data from python which looks something like this: 我有来自python的空间坐标数据,看起来像这样:

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

I am analyzing this data in R, but I don't know how to grab individual coordinates ie how to get to [179.0, -79.0] . 我正在R中分析此数据,但我不知道如何获取单个坐标,即如何获取[179.0, -79.0] I have tried different indexing options but had no luck. 我尝试了不同的索引选项,但没有运气。 Just for context, my dataframe has a column with python list of lists. 仅出于上下文考虑,我的数据框有一列带有python列表列表的列。

Any help would be much appreciated. 任何帮助将非常感激。 Thanks! 谢谢!

Clarification: 澄清:

I was wondering if I can extract individual coordinates from '[[-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. 我想知道是否可以从'[[-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.

This is what the data looks like in R. I loaded a csv file into a dataframe and each observation in df$click_pos has a value like '[[-188.0, -261.0], [-183.0, -30.0], [88.0, -26.0], [11.0, 339.0], [-198.0, 323.0], [-115.0, -8.0]]' 这就是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]]'

So can I extract [-183.0, -30.0] from the above ? 那么我可以从上面提取[-183.0, -30.0]吗?

dfgood['click_pos'][[1]][1] gives 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]] ...   

Note that typeof( dfgood['click_pos'][[1]][1]) gives type int 请注意, typeof( dfgood['click_pos'][[1]][1])类型为int

R data when I do dfgood['click_pos'][[1]] : 我执行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))--trimmed 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")

I had problems recreating your data, so I assumed you're coming from python and your data looked like this: [[179.0, -79.0], [127.0, 301.0], [-92.0, 310.0]] - python style. 我在重新创建数据时遇到问题,因此我假设您来自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样式。

Recreating it using R code: 使用R代码重新创建它:

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

What you have here is a list of lists, and if you'd like to get an item using indexing you need to use [] . 您所拥有的是一个列表列表,如果您想使用索引来获取项目,则需要使用[]

First, in R it's always nice to use the str() function to understand your variable. 首先,在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

We can see that we have a list with 3 list within it. 我们可以看到其中有3个列表。 Next, we can start to get each list/item from the list using [] and indexing (don't forget that unlike python , R uses 1 indexing): 接下来,我们可以开始使用[]和索引来获取列表中的每个列表/项目(别忘了,与python不同, R使用1个索引):

If will type df[1] will get to our first list: 如果输入df[1]将进入我们的第一个列表:

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

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

But if will do df[[1]] will get the first items from our first list: 但是,如果这样做, df[[1]]将从我们的第一个列表中获取第一项:

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

[[2]]
[1] -79

If we'll use a second index like this df[[1]][1] , we'll get our first item in our list: 如果我们使用第二个索引,例如df[[1]][1] ,我们将在列表中获得第一项:

[[1]]
[1] 179

I think that the picture from here and here is very useful: 我认为这里这里的图片非常有用: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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