이 복잡하고 중첩 된 예제를 고려하십시오 (내 데이터 세트에서 실제로 발생하는지 믿거 나 발생하지 않음).
> nestedlist <- list(list('fakedata' = 'hello',
+ list(list('name'= list('john','litz'), 'age'= 30))),
+ list('fakedata' = 'there',
+ list(list('name'= list('frank','doe'), 'age'= 34))))
>
> nestedlist %>% str
List of 2
$ :List of 2
..$ fakedata: chr "hello"
..$ :List of 1
.. ..$ :List of 2
.. .. ..$ name:List of 2
.. .. .. ..$ : chr "john"
.. .. .. ..$ : chr "litz"
.. .. ..$ age : num 30
$ :List of 2
..$ fakedata: chr "there"
..$ :List of 1
.. ..$ :List of 2
.. .. ..$ name:List of 2
.. .. .. ..$ : chr "frank"
.. .. .. ..$ : chr "doe"
.. .. ..$ age : num 34
나는 purrr
그것에서 요소를 추출 하는 데 사용하려고 합니다. fakedata
필드 추출과 같은 간단한 것이 쉽게 작동합니다.
> purrr::map(nestedlist, 'fakedata')
[[1]]
[1] "hello"
[[2]]
[1] "there"
그러나 나는 그 분야에 관심이 name
있습니다. 이 경우 내 purrr
솔루션이 작동하지 않습니다.
> purrr::map(nestedlist, list(1,1,'name'))
[[1]]
NULL
[[2]]
NULL
여기서 문제는 무엇입니까? 감사!
나는의 사용을 제안하려고했고 purrr::pluck()
, 그런 다음 doco를 읽으며 실제로 purrr::map()
.
매우 가깝습니다. map()
문자형 벡터 가 아닌 접근 자 목록을 전달해야하며 놓친 접근자가 있습니다.
nestedlist %>% map( list('data', 1, 'name') )
[[1]]
[[1]][[1]]
[1] "john"
[[1]][[2]]
[1] "litz"
[[2]]
[[2]][[1]]
[1] "frank"
[[2]][[2]]
[1] "doe"
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다