Merge two data.frames into one

Asked

Viewed 5,146 times

1

I have a routine that the goal will always be the same; Every day it should read a file xlsx with all prices (historical series), pull from a given site the prices referring to the last update date, put together these two data in a single data.frame (in order to update the last data) and later turn it into an excel file. Although the webscrappe part is ready, I’m having a hard time putting the two dates together..

My tables are like this:

 head(df.temp)



Data            2        3
 <NA>         codigo  codigo 
 <NA>         nome1   nome2
 2012-01-01    480   330
 ...           ...   ...
 2017-10-03    480   330

And:

itens2
Data        2   3 
2017-04-10 400 300

When I use the function df.melt <- bind_rows(df.temp, itens2) the R returns:

Error in bind_rows_(x, .id) : 
  Can not automatically convert from character to numeric in column "2".

the two tables are data.frames. How to solve?

the ultimate goal would be a table like this:

  Data            2        3
     <NA>         codigo  codigo 
     <NA>         nome1   nome2
     2012-01-01    480   330
     ...           ...   ...
     2017-10-03    480   330
     2017-04-10    400   300
  • Whenever possible, share a part of your database with us. Choose a few lines from it and enter the result of the command dput(MeuBancoDeDados). This will make life much easier for those who are willing to help, as it will not be necessary to create a special data set to try to solve the problem. Maybe this is why Gabe was unable to answer this question.

2 answers

2


The R is saying that it cannot join data frames because they are of different types. Columns of df.temp are not numerical values, whereas itens2 sane.

First, in order to solve this problem, I would organize the datasets, eliminating unnecessary rows and giving names with some meaning to their columns. Here I am assuming that only the first two lines of df.temp present problems. For example,

df.temp        <- df.temp[-(1:2), ]
names(df.temp) <- c("Data", "Nome_1", "Nome_2")

The first line of code eliminates the first two lines of df.temp, because they’re not good for anything. The second line of code names the columns of the data frame for something meaningful. Since I don’t know the goal here, I named them "Name_1" and "Name_2".

Next, it is necessary to do something similar with itens2. There is no missing data here. So just rename the columns of itens2 so that they have the same names as the columns of df.temp:

names(itens2) <- c("Data", "Nome_1", "Nome_2")

Now just put the two objects together:

df.melt <- bind_rows(df.temp, itens2)

1

Error appears because your table has text and numbers in the same column.

df.temp <- df.temp[complete.cases(df.temp),] will disappear with the initial two lines.

To add the new row in your series, the columns of df.temp and itens2 has to be the same class. By the error you reported, when do str(df.temp) and str(itens2), I suppose the categories of df.temp are date, text and text, and itens2 are date, number and number.

To standardize the classes, do df.temp[,2] <- as.numeric(df.temp[,2]) and df.temp[,3] <- as.numeric(df.temp[,3])

After that, you can join the two by doing df.nova <- rbind.data.frame(df.temp, itens2)

Browser other questions tagged

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