Add a column that divides another column into n chunks, R












0















There's no easy way to describe my question, that's probably why I was not able to find answer through search.



So I have a data frame with 3 columns, one of the columns is Subject number, the other two columns are Correctness and Block. There are 2 participants, each was exposed to 2 blocks of 3 stimuli in each block.



     subj corr block
1 1 1 1
2 1 0 1
3 1 1 1
4 1 1 2
5 1 1 2
6 1 1 2
7 2 0 1
8 2 1 1
9 2 1 1
10 2 0 2
11 2 1 2
12 2 1 2


So what I want to do is to create another column that look at a specific subj number and divide the block columns corresponding to the subj into 3 even chunks (the original df has 2 chunks). In general, I want to know how to divide the stimuli each subj is exposed to in to N chunks and input the chunk number into another column.



    subj corr block newblock
1 1 1 1 1
2 1 0 1 1
3 1 1 1 2
4 1 1 2 2
5 1 1 2 3
6 1 1 2 3
7 2 0 1 1
8 2 1 1 1
9 2 1 1 2
10 2 0 2 2
11 2 1 2 3
12 2 1 2 3









share|improve this question




















  • 1





    Really?. I gave up after seeing to over 50,000 lines and the display was still not complete. Read Minimal, Complete, and Verifiable example and reflect on why we might see this as not "M". And questions with no attempt at coding are generally met with resistance to do your entire project for you. SO is not a coding service. We expect you to write code that would download and than make some effort.

    – 42-
    Nov 26 '18 at 2:22











  • Thanks for the suggestions, I've edited my question.

    – Tokaalmighty
    Nov 26 '18 at 3:04
















0















There's no easy way to describe my question, that's probably why I was not able to find answer through search.



So I have a data frame with 3 columns, one of the columns is Subject number, the other two columns are Correctness and Block. There are 2 participants, each was exposed to 2 blocks of 3 stimuli in each block.



     subj corr block
1 1 1 1
2 1 0 1
3 1 1 1
4 1 1 2
5 1 1 2
6 1 1 2
7 2 0 1
8 2 1 1
9 2 1 1
10 2 0 2
11 2 1 2
12 2 1 2


So what I want to do is to create another column that look at a specific subj number and divide the block columns corresponding to the subj into 3 even chunks (the original df has 2 chunks). In general, I want to know how to divide the stimuli each subj is exposed to in to N chunks and input the chunk number into another column.



    subj corr block newblock
1 1 1 1 1
2 1 0 1 1
3 1 1 1 2
4 1 1 2 2
5 1 1 2 3
6 1 1 2 3
7 2 0 1 1
8 2 1 1 1
9 2 1 1 2
10 2 0 2 2
11 2 1 2 3
12 2 1 2 3









share|improve this question




















  • 1





    Really?. I gave up after seeing to over 50,000 lines and the display was still not complete. Read Minimal, Complete, and Verifiable example and reflect on why we might see this as not "M". And questions with no attempt at coding are generally met with resistance to do your entire project for you. SO is not a coding service. We expect you to write code that would download and than make some effort.

    – 42-
    Nov 26 '18 at 2:22











  • Thanks for the suggestions, I've edited my question.

    – Tokaalmighty
    Nov 26 '18 at 3:04














0












0








0








There's no easy way to describe my question, that's probably why I was not able to find answer through search.



So I have a data frame with 3 columns, one of the columns is Subject number, the other two columns are Correctness and Block. There are 2 participants, each was exposed to 2 blocks of 3 stimuli in each block.



     subj corr block
1 1 1 1
2 1 0 1
3 1 1 1
4 1 1 2
5 1 1 2
6 1 1 2
7 2 0 1
8 2 1 1
9 2 1 1
10 2 0 2
11 2 1 2
12 2 1 2


So what I want to do is to create another column that look at a specific subj number and divide the block columns corresponding to the subj into 3 even chunks (the original df has 2 chunks). In general, I want to know how to divide the stimuli each subj is exposed to in to N chunks and input the chunk number into another column.



    subj corr block newblock
1 1 1 1 1
2 1 0 1 1
3 1 1 1 2
4 1 1 2 2
5 1 1 2 3
6 1 1 2 3
7 2 0 1 1
8 2 1 1 1
9 2 1 1 2
10 2 0 2 2
11 2 1 2 3
12 2 1 2 3









share|improve this question
















There's no easy way to describe my question, that's probably why I was not able to find answer through search.



So I have a data frame with 3 columns, one of the columns is Subject number, the other two columns are Correctness and Block. There are 2 participants, each was exposed to 2 blocks of 3 stimuli in each block.



     subj corr block
1 1 1 1
2 1 0 1
3 1 1 1
4 1 1 2
5 1 1 2
6 1 1 2
7 2 0 1
8 2 1 1
9 2 1 1
10 2 0 2
11 2 1 2
12 2 1 2


So what I want to do is to create another column that look at a specific subj number and divide the block columns corresponding to the subj into 3 even chunks (the original df has 2 chunks). In general, I want to know how to divide the stimuli each subj is exposed to in to N chunks and input the chunk number into another column.



    subj corr block newblock
1 1 1 1 1
2 1 0 1 1
3 1 1 1 2
4 1 1 2 2
5 1 1 2 3
6 1 1 2 3
7 2 0 1 1
8 2 1 1 1
9 2 1 1 2
10 2 0 2 2
11 2 1 2 3
12 2 1 2 3






r split






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 3:08







Tokaalmighty

















asked Nov 26 '18 at 2:13









TokaalmightyTokaalmighty

978




978








  • 1





    Really?. I gave up after seeing to over 50,000 lines and the display was still not complete. Read Minimal, Complete, and Verifiable example and reflect on why we might see this as not "M". And questions with no attempt at coding are generally met with resistance to do your entire project for you. SO is not a coding service. We expect you to write code that would download and than make some effort.

    – 42-
    Nov 26 '18 at 2:22











  • Thanks for the suggestions, I've edited my question.

    – Tokaalmighty
    Nov 26 '18 at 3:04














  • 1





    Really?. I gave up after seeing to over 50,000 lines and the display was still not complete. Read Minimal, Complete, and Verifiable example and reflect on why we might see this as not "M". And questions with no attempt at coding are generally met with resistance to do your entire project for you. SO is not a coding service. We expect you to write code that would download and than make some effort.

    – 42-
    Nov 26 '18 at 2:22











  • Thanks for the suggestions, I've edited my question.

    – Tokaalmighty
    Nov 26 '18 at 3:04








1




1





Really?. I gave up after seeing to over 50,000 lines and the display was still not complete. Read Minimal, Complete, and Verifiable example and reflect on why we might see this as not "M". And questions with no attempt at coding are generally met with resistance to do your entire project for you. SO is not a coding service. We expect you to write code that would download and than make some effort.

– 42-
Nov 26 '18 at 2:22





Really?. I gave up after seeing to over 50,000 lines and the display was still not complete. Read Minimal, Complete, and Verifiable example and reflect on why we might see this as not "M". And questions with no attempt at coding are generally met with resistance to do your entire project for you. SO is not a coding service. We expect you to write code that would download and than make some effort.

– 42-
Nov 26 '18 at 2:22













Thanks for the suggestions, I've edited my question.

– Tokaalmighty
Nov 26 '18 at 3:04





Thanks for the suggestions, I've edited my question.

– Tokaalmighty
Nov 26 '18 at 3:04












1 Answer
1






active

oldest

votes


















0














Something like this:



library(dplyr)
n_chunks = 3
df %>%
group_by(subj) %>%
mutate(newblock = rep(1:n_chunks, each = ceiling(n() / n_chunks))[1:n()])


How much of this is necessary depends on your use case. If you can guarantee that n_chunks evenly divides the number of observations for each subject you can simplify to:



df %>% 
group_by(subj) %>%
mutate(newblock = rep(1:n_chunks, each = n() / n_chunks))





share|improve this answer


























  • Thanks @Gregor! There was a missing bracket in the first code block before [1:n()], but otherwise it solved my problem perfectly well. df %>% group_by(subj) %>% mutate(newblock = rep(1:n_chunks, each = ceiling(n() / n_chunks))[1:n()])

    – Tokaalmighty
    Nov 26 '18 at 3:35













  • If anyone else run into the Error 'This function should not be called directly', detach dplyr first with detach("package:dplyr", unload=TRUE), then load plyr first, then load dplyr

    – Tokaalmighty
    Nov 26 '18 at 16:50











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%2f53473953%2fadd-a-column-that-divides-another-column-into-n-chunks-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Something like this:



library(dplyr)
n_chunks = 3
df %>%
group_by(subj) %>%
mutate(newblock = rep(1:n_chunks, each = ceiling(n() / n_chunks))[1:n()])


How much of this is necessary depends on your use case. If you can guarantee that n_chunks evenly divides the number of observations for each subject you can simplify to:



df %>% 
group_by(subj) %>%
mutate(newblock = rep(1:n_chunks, each = n() / n_chunks))





share|improve this answer


























  • Thanks @Gregor! There was a missing bracket in the first code block before [1:n()], but otherwise it solved my problem perfectly well. df %>% group_by(subj) %>% mutate(newblock = rep(1:n_chunks, each = ceiling(n() / n_chunks))[1:n()])

    – Tokaalmighty
    Nov 26 '18 at 3:35













  • If anyone else run into the Error 'This function should not be called directly', detach dplyr first with detach("package:dplyr", unload=TRUE), then load plyr first, then load dplyr

    – Tokaalmighty
    Nov 26 '18 at 16:50
















0














Something like this:



library(dplyr)
n_chunks = 3
df %>%
group_by(subj) %>%
mutate(newblock = rep(1:n_chunks, each = ceiling(n() / n_chunks))[1:n()])


How much of this is necessary depends on your use case. If you can guarantee that n_chunks evenly divides the number of observations for each subject you can simplify to:



df %>% 
group_by(subj) %>%
mutate(newblock = rep(1:n_chunks, each = n() / n_chunks))





share|improve this answer


























  • Thanks @Gregor! There was a missing bracket in the first code block before [1:n()], but otherwise it solved my problem perfectly well. df %>% group_by(subj) %>% mutate(newblock = rep(1:n_chunks, each = ceiling(n() / n_chunks))[1:n()])

    – Tokaalmighty
    Nov 26 '18 at 3:35













  • If anyone else run into the Error 'This function should not be called directly', detach dplyr first with detach("package:dplyr", unload=TRUE), then load plyr first, then load dplyr

    – Tokaalmighty
    Nov 26 '18 at 16:50














0












0








0







Something like this:



library(dplyr)
n_chunks = 3
df %>%
group_by(subj) %>%
mutate(newblock = rep(1:n_chunks, each = ceiling(n() / n_chunks))[1:n()])


How much of this is necessary depends on your use case. If you can guarantee that n_chunks evenly divides the number of observations for each subject you can simplify to:



df %>% 
group_by(subj) %>%
mutate(newblock = rep(1:n_chunks, each = n() / n_chunks))





share|improve this answer















Something like this:



library(dplyr)
n_chunks = 3
df %>%
group_by(subj) %>%
mutate(newblock = rep(1:n_chunks, each = ceiling(n() / n_chunks))[1:n()])


How much of this is necessary depends on your use case. If you can guarantee that n_chunks evenly divides the number of observations for each subject you can simplify to:



df %>% 
group_by(subj) %>%
mutate(newblock = rep(1:n_chunks, each = n() / n_chunks))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 3:39

























answered Nov 26 '18 at 3:15









GregorGregor

67.1k1095179




67.1k1095179













  • Thanks @Gregor! There was a missing bracket in the first code block before [1:n()], but otherwise it solved my problem perfectly well. df %>% group_by(subj) %>% mutate(newblock = rep(1:n_chunks, each = ceiling(n() / n_chunks))[1:n()])

    – Tokaalmighty
    Nov 26 '18 at 3:35













  • If anyone else run into the Error 'This function should not be called directly', detach dplyr first with detach("package:dplyr", unload=TRUE), then load plyr first, then load dplyr

    – Tokaalmighty
    Nov 26 '18 at 16:50



















  • Thanks @Gregor! There was a missing bracket in the first code block before [1:n()], but otherwise it solved my problem perfectly well. df %>% group_by(subj) %>% mutate(newblock = rep(1:n_chunks, each = ceiling(n() / n_chunks))[1:n()])

    – Tokaalmighty
    Nov 26 '18 at 3:35













  • If anyone else run into the Error 'This function should not be called directly', detach dplyr first with detach("package:dplyr", unload=TRUE), then load plyr first, then load dplyr

    – Tokaalmighty
    Nov 26 '18 at 16:50

















Thanks @Gregor! There was a missing bracket in the first code block before [1:n()], but otherwise it solved my problem perfectly well. df %>% group_by(subj) %>% mutate(newblock = rep(1:n_chunks, each = ceiling(n() / n_chunks))[1:n()])

– Tokaalmighty
Nov 26 '18 at 3:35







Thanks @Gregor! There was a missing bracket in the first code block before [1:n()], but otherwise it solved my problem perfectly well. df %>% group_by(subj) %>% mutate(newblock = rep(1:n_chunks, each = ceiling(n() / n_chunks))[1:n()])

– Tokaalmighty
Nov 26 '18 at 3:35















If anyone else run into the Error 'This function should not be called directly', detach dplyr first with detach("package:dplyr", unload=TRUE), then load plyr first, then load dplyr

– Tokaalmighty
Nov 26 '18 at 16:50





If anyone else run into the Error 'This function should not be called directly', detach dplyr first with detach("package:dplyr", unload=TRUE), then load plyr first, then load dplyr

– Tokaalmighty
Nov 26 '18 at 16:50




















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%2f53473953%2fadd-a-column-that-divides-another-column-into-n-chunks-r%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

Wiesbaden

Marschland

Dieringhausen