I’ve got a data frame like this:
id class
146 H02J
146 F03D
146 F03D
287 F16F
287 F16F
1040 F03D
1040 F16D
1040 F03D
1042 F03D
1042 G01W
1042 F03D
1042 F03D
1042 F03D
1816 G06F
1816 H04Q
1816 H04L
1816 H04W
Now I want to build vectors with numeric values, each vector representing one application and each numeric value representing a class
.
Because of different length of the vectors, I cannot define a matrix with the vectors, with my R skills, and I am thankful for ideas to solve this problem.
The output should be a matrix like this, with the goal to determine the distance between the vectors.
> mat
[,1] [,2] [,3] [,4] [,5]
[1,] 6 1 1 NA NA
[2,] 3 3 NA NA NA
[3,] 1 2 1 NA NA
[4,] 1 4 1 1 1
[5,] 5 8 7 9 NA
I got this with:
v1 <- subset(num, id==146)
v2 <- subset(num, id==287)
v3 <- subset(num, id==1040)
v4 <- subset(num, id==1042)
v5 <- subset(num, id==1816)
list <- list(c(v1), c(v2), c(v3), c(v4), c(v5))
list
max.length <- max(sapply(list, length))
list <- lapply(list, function(x) { c(x, rep(NA, max.length-length(x)))})
do.call(rbind, list)
mat <- do.call(rbind, list)
but the solution should not only work for this five examples, but for a huge amount of id
’s (vectors), without put the numbers of id
’s manually.