2013-03-15 2 views
0

3D 배열을 미리 할당하고 데이터로 채 웁니다. 그러나 이전에 정의 된 data.frame collumn을 사용하여이 작업을 수행 할 때마다 배열이 신비하게 목록으로 변환되어 모든 것을 망칩니다. data.frame collumn을 벡터로 변환해도 도움이되지 않습니다.R에 3D 배열 채우기 : 강제 변환을 피하는 방법?

예 :

exampleArray <- array(dim=c(3,4,6)) 
exampleArray[2,3,] <- c(1:6) # direct filling works perfectly 

exampleArray 
str(exampleArray) # output as expected 

문제 :

exampleArray <- array(dim=c(3,4,6)) 
exampleContent <- as.vector(as.data.frame(c(1:6))) 
exampleArray[2,3,] <- exampleContent # filling array from a data.frame column 
# no errors or warnings 

exampleArray  
str(exampleArray) # list-like output! 

나는이 문제를 해결 얻고 일반적으로 내 배열을 채울 수있는 방법이 있나요?

의견을 보내 주셔서 감사합니다.

답변

1

이 시도 : 당신은 작동하지 않을 것이다, 배열에 데이터 프레임을 삽입하려고했던

exampleArray <- array(dim=c(3,4,6)) 
exampleContent <- as.data.frame(c(1:6)) 
> exampleContent[,1] 
[1] 1 2 3 4 5 6 
exampleArray[2,3,] <- exampleContent[,1] # take the desired column 
# no errors or warnings 
str(exampleArray) 
int [1:3, 1:4, 1:6] NA NA NA NA NA NA NA 1 NA NA ... 

. 대신 dataframe$column 또는 dataframe[,1]을 사용해야합니다. 그래도 문제가 해결되지 있지만, 또한

, as.vector는, 당신이 as.vector(as.data.frame(c(1:6))) 후 아마 있었다) as.vector(as.data.frame(c(1:6))에서 아무것도하지 않습니다

as.vector(as.data.frame(c(1:6))) 
Error: (list) object cannot be coerced to type 'double' 
+0

가 좋아, 너무 작은 ","차이를 만든다! 고마워요! – jgoldmann