Merge date.frames into R

Asked

Viewed 55 times

2

Hello.

I have two data.frames. Follow:


head(dadosFiltradosBand)
 rdf.schema.label                                        genre_label
4 The Valentines (rock band)                             {Pop music|Rock music}
5    The Valentino Orchestra                                               NULL
6             The Valentinos {Rhythm and blues|Soul music|Doo-wop|Gospel music}
7        The Valerie Project                                   Psychedelic folk
8          The Valley (band)                   {Alternative rock|Post-hardcore}
9                   The Vals {Folk music|Psychedelic rock|Pop music|Rock music}

head(dadosFiltradosMusicalArtist)
  rdf.schema.label                                             genre_label
4         MC Primo                                            Funk carioca
5     MC Raaka Pee              {Death metal|Gothic rock|Industrial metal}
6          MC Rage                 Hardcore (electronic dance music genre)
7           MC Ren {Gangsta rap|G-funk|Hardcore hip hop|Political hip hop}
8          MC Rene                                                    NULL
9          MC Ride                    {Hip hop music|Experimental hip hop}

I need to assign all data for each of the two data.frames to a new data.frame, where rdf.schema.label and genre_label of dadosFiltradosBand and rdf.schema.label and genre_label of dadosFiltradosMusicalArtist stay one below the other.

Basically, I want to keep the same two columns in the new data frame, I just want to add the data of one data.frame below the other.

I found a lot about, but in order to unite and create two new columns.

  • 2

    novoDF = rbind(dadosFiltradosBand, dadosFiltradosMusicalArtist)?

  • 1

    If @Willianvieira’s suggestion doesn’t work because the columns are class "factor", change them first with as.character().

  • The @Willianvieira suggestion worked. Thank you very much.

1 answer

4


To join the 2 data.frames into rows you can use the function rbind.data.frame or only rbind. However, in your case it will give error saying that the data.frames have different column names. You will need to rename the columns so that they have the same name. The code below should solve your problem:

names(dadosFiltradosMusicalArtist) <- names(dadosFiltradosBand)
novosDados <- rbind.data.frame(dadosFiltradosMusicalArtist, dadosFiltradosBand)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.