Best way to store variable-length data in an R data.frame?

前端 未结 5 1180
日久生厌
日久生厌 2021-02-06 02:59

I have some mixed-type data that I would like to store in an R data structure of some sort. Each data point has a set of fixed attributes which may be 1-d numeric, factors, or

5条回答
  •  借酒劲吻你
    2021-02-06 03:39

    Another option would be to convert your data frame into a matrix of mode list - each element of the matrix would be a list. standard array operations (slicing with [, apply(), etc. would be applicable).

    > d <- data.frame(id=c(1,2,3), num_tokens=c(2,1,4), token_lengths=as.array(list(c(5,5), 9, c(4,2,2,4,6))))
    > m <- as.matrix(d)
    > mode(m)
    [1] "list"
    > m[,"token_lengths"]
    [[1]]
    [1] 5 5
    
    [[2]]
    [1] 9
    
    [[3]]
    [1] 4 2 2 4 6
    
    > m[3,]
    $id
    [1] 3
    
    $num_tokens
    [1] 4
    
    $token_lengths
    [1] 4 2 2 4 6
    

提交回复
热议问题