0

I have two different data frames, one with my measurments of trees in different plots and one data frame with metadata on the plots. The first data frame (df) looks like this:

     NWID PKR  ID ART JAHR_1 BHD_1   VOL_1       GFL_1 JAHR_2 BHD_2   VOL_2       GFL_2
1     802   1 221 110   1988   412  1.7921 0.133315000   2009   445  2.1838 0.155530000
2     802  10  31 110   1988   499  3.0016 0.195560000     NA    NA      NA          NA
3     802  10 181 110   1988   445  2.3128 0.155530000     NA    NA      NA          NA
4     802  12 201 110   1988   417  2.0450 0.136570000   2009   521  3.4686 0.213190000
5     802   2  61 110   1988   243  0.5754 0.046380000     NA    NA      NA          NA
6     802   2  81 110   1988   358  1.3949 0.100660000   2009   432  2.1302 0.146570000
7     802  22 221 110   1988   382  1.5939 0.114610000   2009   439  2.3180 0.151360000
8     802  23 111 110   1988   480  2.7256 0.180960000   2009   534  3.2518 0.223960000
9     802  24 191 110   1988   475  2.6678 0.177210000   2009   562  4.1705 0.248060000
10    802  26  31 110   1988   233  0.5172 0.042640000     NA    NA      NA          NA
11    802  26 141 110   1988   452  2.3141 0.160460000   2009   494  2.6645 0.191670000
12    802  27 131 110   1988   451  2.1150 0.159750000   2009   441  2.0743 0.152750000
13    802  27 171 110   1988   460  2.2117 0.166190000   2009   512  3.1105 0.205890000
14    802   3  41 110   1988   357  1.5672 0.100100000   2009   355  1.4833 0.098980000

The second one (df_M) with the metadata looks like this:

   NWID PKR NEIG TOPO EXPO     HNN FLTYP Nährstoffversorgung Wasserversorgung Stichjahr_FE Alter BGrad
1   802   1    8 OHAN   NW 342.686    TR           mesotroph     mäßig frisch         2012   159  1.08
2   802  10    7 OHAN   NO 348.358    VF           mesotroph     mäßig frisch         2012   159  0.52
3   802  11   12 PLAT   NO 354.880    VF           mesotroph           frisch         2012   159  0.52
4   802  12   11 OHAN    N 361.522    VF           mesotroph     mäßig frisch         2012   159  0.52
5   802  14   20 OHAN   NO 346.793    VF           mesotroph           frisch         2012   159  0.52
6   802  15   15 MHAN    O 335.203    VF           mesotroph           frisch         2012   159  0.52
7   802  16    8 MHAN   NO 334.761    VF           mesotroph     mäßig frisch         2012   159  0.53
8   802  17    7 MHAN    N 327.274    VF           mesotroph     mäßig frisch         2012   159  0.53
9   802  18   19 UHAN    N 323.117    VF           mesotroph           frisch         2012   159  0.52
10  802  19   10 MHAN   NO 337.545    VF           mesotroph           frisch         2012   159  0.52
11  802   2   15 MHAN   NW 335.509    TR           mesotroph     mäßig frisch         2012   159  1.08
12  802  21   10 MHAN   NO 320.003    VF           mesotroph           frisch         2012   159  0.52
13  802  22   12 OHAN    O 326.147    VF           mesotroph           frisch         2012   159  0.53
14  802  23   11 UHAN    O 313.310    VF           mesotroph           frisch         2012   159  0.53

The column PKR in both data frames is the Plot number. In every Plot several trees were measured, that´s why the numbers of PKR appear more than once in df. In df_M PKR only appears once because several different attributes are there defined for each Plot.

What I want to do is add a column to df with the information of df_M$FLTYPwhich can be the factor TR or VF, depending on the PKR number. So the result should show a new column in df, e.g. for all PKR = 1 TR, for all PKR = 10 VF etc. depending on the information in df_M

I hope this question is understandable, it´s my first, so if you need additional details please tell me. I tried a lot but can´t get the right result some help would be great, thanks a lot!!!

TreePete
  • 1
  • 4
  • You probably want to join the two data.frames: `merge(df, df_M, by = "PKR")` and then select appropriate columns. See https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right – ozacha Nov 19 '18 at 15:32
  • Ok thanks a lot! This works just fine, but when I use the code like this, all the columns of `df_M` are added. I can just clear out what I don´need, but is there a way where only `FLTYP` is added in the same line of code? Sry I´m a newbie – TreePete Nov 19 '18 at 16:18
  • `merge(df, df_M[, c('PKR','FLTYP')], by = "PKR")` should work – Chris Nov 19 '18 at 20:19
  • That worked perfectly, thanks a lot! – TreePete Nov 19 '18 at 22:48

0 Answers0