Save vectors as records in a data frame?

I am new to R, and I was wondering if there is a way to store vectors in a data frame so that each element of the data frame is a vector.

I want to assign a record to a vector and be able to change it later:

df[2,]$Value <- c(1,2,0)
df[3,]$Value <- c(0,0,1)
df[3,]$Value <- df[3,]$Value + c(2,4,4)

But can only do this job by replacing 3 different records in the data frame. Does anyone know if this is possible?

df: 
      V1    V2     V3 
1 c(1,2) c(2,3) c(0,0) 
2 c(1,1) c(0,0) c(2,2)
+5
source share
1 answer

You cannot have a three-dimensional data frame. You can achieve this in many ways, but the easiest way to explain it conceptually is likely to have a list in the list:

> entries <- list(V1=list(c(1,2), c(1,1)), V2=list(c(2,3), c(0,0)), V3=list(c(0,0),c(2,2)))
> entries
$V1
$V1[[1]]
[1] 1 2

$V1[[2]]
[1] 1 1


$V2
$V2[[1]]
[1] 2 3

$V2[[2]]
[1] 0 0


$V3
$V3[[1]]
[1] 0 0

$V3[[2]]
[1] 2 2

Now let's say you want to change the second record of V1, which you would just do

entries $ V1 [[2]] <- c (2,2)

or

records [[1]] [[2]] <- c (2,2)

+3
source

All Articles