Insert vector value from a data.frame into another data.frame [duplicate]

1

I have two data frames. The first one has 2185 observations and the second has only 9.

The first data frame whose name is dados contains the following variables: local , parc , arv , DAP , Ht , Hf , epiteto genero .

The second, with coordg name contains parcela , longitude , latitude and Eg .

I would like to insert the values of the environment variable Eg of the data frame coordg into the data frame dados , repeating the value of Eg for each arv , according to the name of parc in common between the data frames, as below:

   Local    parc   arv  DAP     Ht    Hf   copa      genero       epiteto      Eg
  varzea    MZVT2   1   11.777  14.1  13.5  0.6      Euterpe       oleracea    0.016
  varzea    MZVT2   2   13.782  17.4  14.0  3.4      Euterpe       oleracea    0.016
  varzea    MZVT2   3    9.326  11.1   4.0  7.1      Euterpe       oleracea    0.016
  varzea    MZVT2   4   25.305  20.8  12.0  8.8   Calycophyllum  spruceanum    0.016
  varzea    MZVT2   5   10.345  11.2   4.0  7.2   Indeterminada Indeterminada  0.016
    
asked by anonymous 12.09.2017 / 03:38

2 answers

0

First I suggest to change the name of the column parc in parc, to be equal in both dataframes:

names(coordg)[1] <- "parc"

The merge function can help you. The code looks like this:

dados2 <- merge(dados, coordg[ ,c("parc","Eg")], by="parc")

Remembering that dados 2 will be in the new dataframe with all columns of dados plus Eg of each parc .

    
13.09.2017 / 14:20
0

You can use the subset function to check which value of the parc you need to use:

subset(coordg, parcela %in% dados$parc)

This will return the required value of the parcela field of the dataframe coordg

So you just need to add a new column in the dataframe and fill it with this value:

dados$eg <- subset(coordg, parcela %in% dados$parc)[4]

Or even

dados$eg <- subset(coordg, parcela %in% dados$parc)["Eg"]

Given that the number 4 refers to the number of the column in which the eg values in the% dataframe coordg are.

This will only work when the dataframe dados has only the same value for parc on all lines. If it has different values, it will need to be modified (I think a for could already solve if it was necessary). If this is the case, please comment that I will try to figure out how to resolve it.

    
12.09.2017 / 05:15