2

Let's say I have two data. one is like this:

names |height
------|------
"ab"  |176
------|--------
"aa"  |168
------|--------
"ac"  |189
------|--------
"ad"  |179

and the other is like this:

names           weight
c("aa","ab")    58
c("ac","ae")    70
"ad"            68

so the second names are list, but the first names are just vector. I want to make like this:

names  height  weight
"ab"   176     58
"aa"   168     58
"ac"   189     70
"ad"   179     68

I tried to use left_join, but it didn't work. And I also tried to make list to vector. When I made list to vector, the problem is lengths are different each other. Please, can you help me??? This is my first question on stackoverflow.

Add my code

names<-c("ab","aa","ac","ad")
height<-c(176,168,189,179)
data1<-cbind(names,height)
names<-list(c("aa","ab"),c("ac","ae"),"ad")
weight<-c(58,70,68)
data2<-cbind(names,weight)
data1<-as.data.frame(data1) ;data1;str(data1)
data2<-as.data.frame(data2) ;data2;str(data2)
data2 %>%

unnest %>% left_join(.,data1, by = "names")

Dougie Hwang
  • 93
  • 1
  • 1
  • 9
  • Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 Sep 01 '17 at 06:29
  • Please share your code even if it didn't work. – zx8754 Sep 01 '17 at 06:30
  • 1
    ok, I will edit more with some code. – Dougie Hwang Sep 01 '17 at 06:32
  • oh, it just automatically changed itself!!!! – Dougie Hwang Sep 01 '17 at 06:32
  • You are creating a matrix with `cbind` and this will create more problems for you as matrix can take only a single class. So it converts to numeric to character because there is character elements and then you need to go the reconversion etc.. Instead use `data1 <- data.frame(names, height)` and `data2 <- data.frame(names = I(names), weight)` – akrun Sep 01 '17 at 06:59
  • I really appreciate you!! thanks again!! – Dougie Hwang Sep 01 '17 at 07:05
  • Could I ask one more question?? In the second data frame, if the first element have two names like example and the second element have three names unlike above example, then how could I work with that?? – Dougie Hwang Sep 01 '17 at 07:12

1 Answers1

2

We can use tidyverse assuming that the 'names' column is a list in the second dataset. With unnest, convert it to a vector and then left_join with the first dataset by 'names'

library(tidyverse)
df2 %>%
   unnest %>%
   left_join(df1, ., by = "names") 
#   names height weight
#1    ab    176     58
#2    aa    168     58
#3    ac    189     70
#4    ad    179     68

data

df1 <- data.frame(names = c("ab", "aa", "ac", "ad"),
  height = c(176, 168, 189, 179), stringsAsFactors = FALSE)

df2 <- data.frame(names = I(list(c("aa", "ab"), c("ac", "ae"), "ad")),
  weight = c(58, 70, 68))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Could you explain more?? What is unnest?? – Dougie Hwang Sep 01 '17 at 06:38
  • @DosanHwang I added some explanation `unnest` is a function from `tidyr` which is one of the packages that gets loaded with `library(tidyverse)` – akrun Sep 01 '17 at 06:38
  • I'm sorry, but I get an error. Error in UseMethod("unnest_") : no applicable method for 'unnest_' applied to an object of class "c('matrix', 'list')" How should I do??? – Dougie Hwang Sep 01 '17 at 06:47
  • @DosanHwang Looks like you have a `matrix` for `df2` convert it to a `data.frame` i..e `df2 %>% as.data.frame %>% unnest` and check if that works – akrun Sep 01 '17 at 06:48
  • Now I get this error. Error: All nested columns must have the same number of elements. – Dougie Hwang Sep 01 '17 at 06:50
  • @DosanHwang I assumed that you have a `list` column in 'df2' and df2 is a data.frame. Could you please update your post with the `dput` of the example i.e. `dput(df2)` – akrun Sep 01 '17 at 06:51
  • @DosanHwang I updated with the example I used. It is giving the expected output as you showed – akrun Sep 01 '17 at 06:57