를 적용하면 I가 벡터 v
매트릭스 m
있고 (v
및 m
열 사이의 상관 관계) cor.test
함수 결과의 서브 세트를 추출하는 발라 중첩리스트의 구조를 제어. 상기 구조 I 출력 (2 레벨에서)로하고자 -FUN 반환 목록 또는 NA
set.seed(1)
m <- matrix(runif(12), nrow = 3)
v <- 3:1
res <- apply(m, 2, function(x) {
cor.test(x, v, method = 'spearman', exact = F)[c(1,3,4)]
})
이 m
의 열들의 수와 동일한 길이를 갖는리스트의 중첩 된리스트이다. 루프를 적용하고 필터링 된 결과 (여기서는 네 결과의 길이를 유지) 나타내는데 NA를 반환 내에 I 각 cor.test
결과를 필터링 할
> str(res)
List of 4
$ :List of 3
..$ statistic: Named num 8
.. ..- attr(*, "names")= chr "S"
..$ p.value : num 0
..$ estimate : Named num -1
.. ..- attr(*, "names")= chr "rho"
$ :List of 3
..$ statistic: Named num 2
.. ..- attr(*, "names")= chr "S"
..$ p.value : num 0.667
..$ estimate : Named num 0.5
.. ..- attr(*, "names")= chr "rho"
$ :List of 3
..$ statistic: Named num 0
.. ..- attr(*, "names")= chr "S"
..$ p.value : num 0
..$ estimate : Named num 1
.. ..- attr(*, "names")= chr "rho"
$ :List of 3
..$ statistic: Named num 6
.. ..- attr(*, "names")= chr "S"
..$ p.value : num 0.667
..$ estimate : Named num -0.5
.. ..- attr(*, "names")= chr "rho"
는 상기 p.value 말한다.
res <- apply(m, 2, function(x) {
tmp <- cor.test(x, v, method = 'spearman', exact = F)[c(1,3,4)]
ifelse(tmp$p.value < 0.1, list(tmp), NA)
})
내 문제는 우리가 지금 3 레벨리스트 구조를 얻을 수 있다는 것입니다
res2 <- apply(m, 2, function(x) {
tmp <- cor.test(x, v, method = 'spearman', exact = F)[c(1,3,4)]
ifelse(tmp$p.value < 0.1, list(tmp), NA)
})
> str(res2)
List of 4
$ :List of 1
..$ :List of 3
.. ..$ statistic: Named num 8
.. .. ..- attr(*, "names")= chr "S"
.. ..$ p.value : num 0
.. ..$ estimate : Named num -1
.. .. ..- attr(*, "names")= chr "rho"
$ : logi NA
$ :List of 1
..$ :List of 3
.. ..$ statistic: Named num 0
.. .. ..- attr(*, "names")= chr "S"
.. ..$ p.value : num 0
.. ..$ estimate : Named num 1
.. .. ..- attr(*, "names")= chr "rho"
$ : logi NA
만 apply
에서 첫 번째 결과가 결과 구조는 다음 필터링되지 않은 들어갈 수 apply
때문에 분명히 같은 요구되는 NA 경우 구조에 대한 결과.
res3 <- apply(m, 2, function(x) {
tmp <- cor.test(x, v, method = 'spearman', exact = F)[c(1,3,4)]
ifelse(tmp$p.value > 0.1, list(tmp), NA) #'invert' the test
})
>res3
List of 4
$ : logi NA
$ :List of 3
..$ statistic: Named num 2
.. ..- attr(*, "names")= chr "S"
..$ p.value : num 0.667
..$ estimate : Named num 0.5
.. ..- attr(*, "names")= chr "rho"
$ : logi NA
$ :List of 3
..$ statistic: Named num 6
.. ..- attr(*, "names")= chr "S"
..$ p.value : num 0.667
..$ estimate : Named num -0.5
.. ..- attr(*, "names")= chr "rho"
내가 헛되이 ifelse(tmp$p.value < 0.1, tmp, NA)
및 ifelse(tmp$p.value < 0.1, list(tmp), list(NA))
를 반환하는 것을 시도했다.
내가 찾은 유일한 해결책은 apply
외부 NA
을 할당하는 것입니다 : 분명히
res4 <- apply(m, 2, function(x) {
cor.test(x, v, method = 'spearman', exact = F)[c(1,3,4)]
})
res4[sapply(res4, "[[", 2) > 0.1] <- NA
, I 적용의 내부 동작에 대해 뭔가를 그리워.
나는 높게보고 추천 '빗자루'패키지는 추출 전에 깔끔한 데이터 프레임으로 결과를 단순화합니다. –