Storing numeric vectors (or arrays) in fields of an R data frame

To store variable-length data vectors and corresponding single-value data in separate fields of the same row of an R data frame, add a new modeless column then populate it:

> bar$cwidth <- NA 
> emptyrow <- data.frame(matrix(rep.int(NA,length(bar)),ncol=length(bar))) 
> names(emptyrow) <- c("abund","cwidth") 
> bar <- rbind(bar,emptyrow) 
> bar$abund[3] <- 3 
> bar$cwidth[3] <- list(c(6,0.3,4)) 
> str(bar)
'data.frame': 3 obs. of  2 variables:
$ abund : num  3 4 3
$ cwidth:List of 3
..$ : logi  NA
..$ : logi  NA
..$ : num  6 0.3 4
>

R recognizes that the vector’s class is numeric, but it is necessary to store it as mode list. If the column were mode array, then the dimension would have to be fixed across all rows of the data frame, and therefore not the best fit for irregular data. It is possible to store each vector as array, though; just create and rbind the new row, then populate with the array as list:

> bar$cwidth[nrow(bar)] <- list(as.array(c(6,0.3,4))) 
> str(bar)
'data.frame': 3 obs. of  2 variables:
$ abund : num  3 4 3
$ cwidth:List of 3
..$ : logi  NA
..$ : logi  NA
..$ : num [1:3(1d)] 6 0.3 4
>
Advertisements
This entry was posted in data munging, R and tagged , , , , . Bookmark the permalink.