creating two variables based on selective elimination and pair combinations










1














I have a data of the following form. The data has information about three projects- 1, 2,3 and I would like to calculate the trips of colleagues and purchases till prior year but I want to eliminate all the projects in which he is taking part till that point(and only consider projects in which he is not part of). For example, see below:



 id<- c(1,1,2,2,2,3,3)
person<- c('John','Kevin', 'John', 'Matt', 'Alvin', 'Kevin', 'Alvin')
trips<- c(4,2,5,2,3,4,5)
purchase<- c(6,4,3,4,5,5,6)
join_year<- c(2009,2008,2008,2007,2007, 2008,2008)
df<- data.frame(id, person, trips, purchase, year)


I would like to create two variables called pr_trips_colleagues (referring to prior year) and pr_purchases_colleagues(referring to prior year). I would like to have a final output as follows:



 person year pr_trips_colleagues pr_purchase_colleagues 
John 2009 5 6
John 2008 0 0
John 2007 0 0
Kevin 2009 8 8
Kevin 2008 0 0
Kevin 2007 0 0
Matt 2009 5 6
Matt 2008 0 0
Matt 2007 0 0
Alvin 2009 2 4
Alvin 2008 0 0
Alvin 2007 0 0


Please provide suggestions on how to obtain this? Thanks so much.










share|improve this question





















  • I changed that to join year to indicate that he joined in that year
    – user3570187
    Nov 12 '18 at 22:31






  • 1




    You are right it should be same across..
    – user3570187
    Nov 12 '18 at 22:32






  • 1




    I've read this 3 times and I have no idea how you are getting that result from that input. Can you give any more detail on how the values in the final output are calculated?
    – thelatemail
    Nov 12 '18 at 22:38











  • For example, let’s take John, he has participated in two projects 1 and 2 though he joined 1 in 2009 and 2 in 2008, so for calculating the 2007, he will 0 and 0 as he did not have colleagues till 2006, then for 2008, we need to look his activities till 2007, so the values won’t change as he did not join any project, but for 2009, we need to look at his projects till 2008, he took part in project 2, so Matt and Alvin became colleagues, and Alvin took part in project 3 in which John was not part of, hence the values become 5 and 6 respectively, Kevin became colleague to John in 2009 so not incl
    – user3570187
    Nov 12 '18 at 22:51










  • Replace the word till with in in the comment above
    – user3570187
    Nov 12 '18 at 22:52















1














I have a data of the following form. The data has information about three projects- 1, 2,3 and I would like to calculate the trips of colleagues and purchases till prior year but I want to eliminate all the projects in which he is taking part till that point(and only consider projects in which he is not part of). For example, see below:



 id<- c(1,1,2,2,2,3,3)
person<- c('John','Kevin', 'John', 'Matt', 'Alvin', 'Kevin', 'Alvin')
trips<- c(4,2,5,2,3,4,5)
purchase<- c(6,4,3,4,5,5,6)
join_year<- c(2009,2008,2008,2007,2007, 2008,2008)
df<- data.frame(id, person, trips, purchase, year)


I would like to create two variables called pr_trips_colleagues (referring to prior year) and pr_purchases_colleagues(referring to prior year). I would like to have a final output as follows:



 person year pr_trips_colleagues pr_purchase_colleagues 
John 2009 5 6
John 2008 0 0
John 2007 0 0
Kevin 2009 8 8
Kevin 2008 0 0
Kevin 2007 0 0
Matt 2009 5 6
Matt 2008 0 0
Matt 2007 0 0
Alvin 2009 2 4
Alvin 2008 0 0
Alvin 2007 0 0


Please provide suggestions on how to obtain this? Thanks so much.










share|improve this question





















  • I changed that to join year to indicate that he joined in that year
    – user3570187
    Nov 12 '18 at 22:31






  • 1




    You are right it should be same across..
    – user3570187
    Nov 12 '18 at 22:32






  • 1




    I've read this 3 times and I have no idea how you are getting that result from that input. Can you give any more detail on how the values in the final output are calculated?
    – thelatemail
    Nov 12 '18 at 22:38











  • For example, let’s take John, he has participated in two projects 1 and 2 though he joined 1 in 2009 and 2 in 2008, so for calculating the 2007, he will 0 and 0 as he did not have colleagues till 2006, then for 2008, we need to look his activities till 2007, so the values won’t change as he did not join any project, but for 2009, we need to look at his projects till 2008, he took part in project 2, so Matt and Alvin became colleagues, and Alvin took part in project 3 in which John was not part of, hence the values become 5 and 6 respectively, Kevin became colleague to John in 2009 so not incl
    – user3570187
    Nov 12 '18 at 22:51










  • Replace the word till with in in the comment above
    – user3570187
    Nov 12 '18 at 22:52













1












1








1







I have a data of the following form. The data has information about three projects- 1, 2,3 and I would like to calculate the trips of colleagues and purchases till prior year but I want to eliminate all the projects in which he is taking part till that point(and only consider projects in which he is not part of). For example, see below:



 id<- c(1,1,2,2,2,3,3)
person<- c('John','Kevin', 'John', 'Matt', 'Alvin', 'Kevin', 'Alvin')
trips<- c(4,2,5,2,3,4,5)
purchase<- c(6,4,3,4,5,5,6)
join_year<- c(2009,2008,2008,2007,2007, 2008,2008)
df<- data.frame(id, person, trips, purchase, year)


I would like to create two variables called pr_trips_colleagues (referring to prior year) and pr_purchases_colleagues(referring to prior year). I would like to have a final output as follows:



 person year pr_trips_colleagues pr_purchase_colleagues 
John 2009 5 6
John 2008 0 0
John 2007 0 0
Kevin 2009 8 8
Kevin 2008 0 0
Kevin 2007 0 0
Matt 2009 5 6
Matt 2008 0 0
Matt 2007 0 0
Alvin 2009 2 4
Alvin 2008 0 0
Alvin 2007 0 0


Please provide suggestions on how to obtain this? Thanks so much.










share|improve this question













I have a data of the following form. The data has information about three projects- 1, 2,3 and I would like to calculate the trips of colleagues and purchases till prior year but I want to eliminate all the projects in which he is taking part till that point(and only consider projects in which he is not part of). For example, see below:



 id<- c(1,1,2,2,2,3,3)
person<- c('John','Kevin', 'John', 'Matt', 'Alvin', 'Kevin', 'Alvin')
trips<- c(4,2,5,2,3,4,5)
purchase<- c(6,4,3,4,5,5,6)
join_year<- c(2009,2008,2008,2007,2007, 2008,2008)
df<- data.frame(id, person, trips, purchase, year)


I would like to create two variables called pr_trips_colleagues (referring to prior year) and pr_purchases_colleagues(referring to prior year). I would like to have a final output as follows:



 person year pr_trips_colleagues pr_purchase_colleagues 
John 2009 5 6
John 2008 0 0
John 2007 0 0
Kevin 2009 8 8
Kevin 2008 0 0
Kevin 2007 0 0
Matt 2009 5 6
Matt 2008 0 0
Matt 2007 0 0
Alvin 2009 2 4
Alvin 2008 0 0
Alvin 2007 0 0


Please provide suggestions on how to obtain this? Thanks so much.







r dplyr plyr tidyr






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 22:27









user3570187

610621




610621











  • I changed that to join year to indicate that he joined in that year
    – user3570187
    Nov 12 '18 at 22:31






  • 1




    You are right it should be same across..
    – user3570187
    Nov 12 '18 at 22:32






  • 1




    I've read this 3 times and I have no idea how you are getting that result from that input. Can you give any more detail on how the values in the final output are calculated?
    – thelatemail
    Nov 12 '18 at 22:38











  • For example, let’s take John, he has participated in two projects 1 and 2 though he joined 1 in 2009 and 2 in 2008, so for calculating the 2007, he will 0 and 0 as he did not have colleagues till 2006, then for 2008, we need to look his activities till 2007, so the values won’t change as he did not join any project, but for 2009, we need to look at his projects till 2008, he took part in project 2, so Matt and Alvin became colleagues, and Alvin took part in project 3 in which John was not part of, hence the values become 5 and 6 respectively, Kevin became colleague to John in 2009 so not incl
    – user3570187
    Nov 12 '18 at 22:51










  • Replace the word till with in in the comment above
    – user3570187
    Nov 12 '18 at 22:52
















  • I changed that to join year to indicate that he joined in that year
    – user3570187
    Nov 12 '18 at 22:31






  • 1




    You are right it should be same across..
    – user3570187
    Nov 12 '18 at 22:32






  • 1




    I've read this 3 times and I have no idea how you are getting that result from that input. Can you give any more detail on how the values in the final output are calculated?
    – thelatemail
    Nov 12 '18 at 22:38











  • For example, let’s take John, he has participated in two projects 1 and 2 though he joined 1 in 2009 and 2 in 2008, so for calculating the 2007, he will 0 and 0 as he did not have colleagues till 2006, then for 2008, we need to look his activities till 2007, so the values won’t change as he did not join any project, but for 2009, we need to look at his projects till 2008, he took part in project 2, so Matt and Alvin became colleagues, and Alvin took part in project 3 in which John was not part of, hence the values become 5 and 6 respectively, Kevin became colleague to John in 2009 so not incl
    – user3570187
    Nov 12 '18 at 22:51










  • Replace the word till with in in the comment above
    – user3570187
    Nov 12 '18 at 22:52















I changed that to join year to indicate that he joined in that year
– user3570187
Nov 12 '18 at 22:31




I changed that to join year to indicate that he joined in that year
– user3570187
Nov 12 '18 at 22:31




1




1




You are right it should be same across..
– user3570187
Nov 12 '18 at 22:32




You are right it should be same across..
– user3570187
Nov 12 '18 at 22:32




1




1




I've read this 3 times and I have no idea how you are getting that result from that input. Can you give any more detail on how the values in the final output are calculated?
– thelatemail
Nov 12 '18 at 22:38





I've read this 3 times and I have no idea how you are getting that result from that input. Can you give any more detail on how the values in the final output are calculated?
– thelatemail
Nov 12 '18 at 22:38













For example, let’s take John, he has participated in two projects 1 and 2 though he joined 1 in 2009 and 2 in 2008, so for calculating the 2007, he will 0 and 0 as he did not have colleagues till 2006, then for 2008, we need to look his activities till 2007, so the values won’t change as he did not join any project, but for 2009, we need to look at his projects till 2008, he took part in project 2, so Matt and Alvin became colleagues, and Alvin took part in project 3 in which John was not part of, hence the values become 5 and 6 respectively, Kevin became colleague to John in 2009 so not incl
– user3570187
Nov 12 '18 at 22:51




For example, let’s take John, he has participated in two projects 1 and 2 though he joined 1 in 2009 and 2 in 2008, so for calculating the 2007, he will 0 and 0 as he did not have colleagues till 2006, then for 2008, we need to look his activities till 2007, so the values won’t change as he did not join any project, but for 2009, we need to look at his projects till 2008, he took part in project 2, so Matt and Alvin became colleagues, and Alvin took part in project 3 in which John was not part of, hence the values become 5 and 6 respectively, Kevin became colleague to John in 2009 so not incl
– user3570187
Nov 12 '18 at 22:51












Replace the word till with in in the comment above
– user3570187
Nov 12 '18 at 22:52




Replace the word till with in in the comment above
– user3570187
Nov 12 '18 at 22:52

















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%2f53271004%2fcreating-two-variables-based-on-selective-elimination-and-pair-combinations%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53271004%2fcreating-two-variables-based-on-selective-elimination-and-pair-combinations%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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党