How to add column with factor to data.frame depending on factor in different data.frame












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!!!










share|improve this question























  • You probably want to join the two data.frames: merge(df, df_M, by = "PKR") and then select appropriate columns. See stackoverflow.com/questions/1299871/…

    – 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















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!!!










share|improve this question























  • You probably want to join the two data.frames: merge(df, df_M, by = "PKR") and then select appropriate columns. See stackoverflow.com/questions/1299871/…

    – 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












0








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!!!










share|improve this question














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!!!







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 14:55









TreePeteTreePete

12




12













  • You probably want to join the two data.frames: merge(df, df_M, by = "PKR") and then select appropriate columns. See stackoverflow.com/questions/1299871/…

    – 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



















  • You probably want to join the two data.frames: merge(df, df_M, by = "PKR") and then select appropriate columns. See stackoverflow.com/questions/1299871/…

    – 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

















You probably want to join the two data.frames: merge(df, df_M, by = "PKR") and then select appropriate columns. See stackoverflow.com/questions/1299871/…

– ozacha
Nov 19 '18 at 15:32





You probably want to join the two data.frames: merge(df, df_M, by = "PKR") and then select appropriate columns. See stackoverflow.com/questions/1299871/…

– 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





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





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





That worked perfectly, thanks a lot!

– TreePete
Nov 19 '18 at 22:48












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377219%2fhow-to-add-column-with-factor-to-data-frame-depending-on-factor-in-different-dat%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377219%2fhow-to-add-column-with-factor-to-data-frame-depending-on-factor-in-different-dat%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()