2014-12-13 4 views
0

"ngrams"및 "pred"라는 2 개의 열이있는 빈 데이터 프레임을 만들고 싶습니다.목록에 원소로리스트를 작성하십시오

df <- data.frame(nGrams=character(), pred = character(), stringsAsFactors=FALSE) 

난 워드 벡터로 "PRED"열의 각 요소를 필요로하지만, I '는 PRED =리스트()를 초기화하는 경우, 데이터 프레임에서 그 열을 추가하지 않을 것이다.

I을 시도했습니다

> pred 
[1] "a" "the" "not" "that" "to" "an" 

df[nrow(df)+1, ] <- c("is", pred) 

Error in matrix(value, n, p) : 
    (converted from warning) data length [7] is not a sub-multiple or multiple of the number of columns [2] 

df[nrow(df)+1, ] <- c("the", list(pred)) 

Error in `[<-.data.frame`(`*tmp*`, nrow(df) + 1, , value = list("the", : 
     (converted from warning) replacement element 2 has 6 rows to replace 1 rows 

누구나 저에게 올바른 방법이 무엇인지 보여줄 수 있습니까? 미리 감사드립니다.

편집 나는 list_pred이 목록의 목록입니다 data.table

dt <- data.table(nGrams = my_ngrams, pred = list_pred) 

를 사용하여 솔루션을 얻었다. 그러나 데이터 프레임을위한 올바른 방법을 아는 것은 여전히 ​​좋습니다.

답변

0

당신은 시도 할 수

d1 <- data.frame(nGrams='the', pred=I(list(pred))) 
str(d1) 
#'data.frame': 1 obs. of 2 variables: 
#$ nGrams: Factor w/ 1 level "the": 1 
#$ pred :List of 1 
# ..$ : chr "a" "the" "not" "that" ... 
#..- attr(*, "class")= chr "AsIs" 

또는 빈 dataframe df

df[nrow(df)+1,] <- list('is', list(pred)) 
str(df) 
#'data.frame': 1 obs. of 2 variables: 
#$ nGrams: chr "is" 
#$ pred :List of 1 
# ..$ : chr "a" "the" "not" "that" ... 

를 사용하여,

pred <- c('a', 'the', 'not', 'that', 'to', 'an') 
관련 문제