Join two data.frames in one

0

I have a routine that the goal will always be the same; Every day it should read a xlsx file with all prices (historical series), pull a given site from the prices referring to the last update date, merge these two data into a single data.frame (in order to update the last one given) and then transform it into an excel file. Although the part of webscrappe is ready, I am having difficulty joining the two data.frames.

My tables look like this:

 head(df.temp)



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

E:

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".

Both tables are data.frames. How to solve?

The end 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
    
asked by anonymous 11.04.2017 / 22:58

2 answers

1

R is saying that it can not merge the frames because they are of different types. Columns of df.temp are not numeric values, while itens2 are.

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

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

The first line of code deletes the first two lines of df.temp , since they are useless. The second line of code names the columns of the data frame for something with meaning. As I do not know the purpose here, I named them as "Name_1" and "Name_2".

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

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

Now just put the two objects together:

df.melt <- bind_rows(df.temp, itens2)
    
13.04.2017 / 13:51
0

The 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 two initial lines.

To add the new row in your series, the columns of df.temp and itens2 must be of the same class. For the error you reported, when I make str(df.temp) and str(itens2) , I assume that the df.temp columns are date, text, and text, and itens2 are date, number, and number.

To standardize classes, make 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)

    
12.04.2017 / 03:16