Home > Mobile >  R merging a dataframe with a vector
R merging a dataframe with a vector

Time:06-30

I have a dataframe df that looks like this:

  indx          adj_coords
1    1    2, 3, 4, 5, 6, 7
2    2   1, 3, 7, 8, 9, 10
3    3 1, 2, 4, 10, 11, 12
4    4 1, 3, 5, 12, 13, 14
5    5 1, 4, 6, 14, 15, 16
6    6 1, 5, 7, 16, 17, 18

I also have a vector vec that looks like this:

vec<-c(1,4,5,3,1)

I would like to get a dataframe of length 5 where each row has the adj_coords of the indx given in vec. It should look something like:

vec adj_coords
1   2, 3, 4, 5, 6, 7
4   1, 3, 5, 12, 13, 14
5   1, 4, 6, 14, 15, 16
3   1, 2, 4, 10, 11, 12
1   2, 3, 4, 5, 6, 7

After that I would like to sample adj_coords so that I have something like:

vec adj_coords              sampled_adj_coords
1   2, 3, 4, 5, 6, 7        3
4   1, 3, 5, 12, 13, 14     5
5   1, 4, 6, 14, 15, 16     14
3   1, 2, 4, 10, 11, 12     11
1   2, 3, 4, 5, 6, 7        6

CodePudding user response:

tried something for you... see if something similar you are looking for...

vec <- c(1,4,5,3,1)
vec <- data.frame("vec"=vec, indx=vec)

df <- structure(list(indx = 1:6, adj_coords = list(2:7, c(1L, 3L, 7L, 8L, 9L, 10L), c(1L, 2L, 4L, 10L, 11L, 12L), c(1L, 3L, 5L, 12L, 13L, 14L), c(1L, 4L, 6L, 14L, 15L, 16L), c(1L, 5L, 7L, 16L, 17L, 18L))), row.names = c(NA, 6L), class = "data.frame")

library(dplyr)
inner_join(vec, df, by = 'indx')

results:

  vec indx          adj_coords
1   1    1    2, 3, 4, 5, 6, 7
2   4    4 1, 3, 5, 12, 13, 14
3   5    5 1, 4, 6, 14, 15, 16
4   3    3 1, 2, 4, 10, 11, 12
5   1    1    2, 3, 4, 5, 6, 7

Just drop the column that is not needed...

CodePudding user response:

Another option:

df <- df[vec,]

Output:

    indx          adj_coords
1      1    2, 3, 4, 5, 6, 7
4      4 1, 3, 5, 12, 13, 14
5      5 1, 4, 6, 14, 15, 16
3      3 1, 2, 4, 10, 11, 12
1.1    1    2, 3, 4, 5, 6, 7

For the random sample you can use this:

df$sampled_adj_coords <- apply(df[-1], 1, function(x) {sample(unlist(x), 1)})

Output:

    indx          adj_coords sampled_adj_coords
1      1    2, 3, 4, 5, 6, 7                  2
4      4 1, 3, 5, 12, 13, 14                 12
5      5 1, 4, 6, 14, 15, 16                  4
3      3 1, 2, 4, 10, 11, 12                  2
1.1    1    2, 3, 4, 5, 6, 7                  3
  •  Tags:  
  • r
  • Related