adding rows to data table using for loop in R
I am playing with some basic R functions and am trying to create a table that includes the Remainder and Quotient for the numbers 1:20
First I created this function:
Find_Remains = function(number, divisor){
Remainder = number%%divisor
Quotient = number%/%divisor
a = c(number, divisor, Remainder, Quotient)
return(a)
}
I add a blank table
final.data.frame = data.frame(matrix(NA, nrow = 1, ncol = 4))
Then I created this for loop
for(i in c(1:20)){
b[i] = Find_Remains(i,5)
final.data.frame = rbind(b[i])
}
the error I get is:
number of items to replace is not a multiple of replacement length
any help would be appreciated. Thanks
r dataframe for-loop division
add a comment |
I am playing with some basic R functions and am trying to create a table that includes the Remainder and Quotient for the numbers 1:20
First I created this function:
Find_Remains = function(number, divisor){
Remainder = number%%divisor
Quotient = number%/%divisor
a = c(number, divisor, Remainder, Quotient)
return(a)
}
I add a blank table
final.data.frame = data.frame(matrix(NA, nrow = 1, ncol = 4))
Then I created this for loop
for(i in c(1:20)){
b[i] = Find_Remains(i,5)
final.data.frame = rbind(b[i])
}
the error I get is:
number of items to replace is not a multiple of replacement length
any help would be appreciated. Thanks
r dataframe for-loop division
add a comment |
I am playing with some basic R functions and am trying to create a table that includes the Remainder and Quotient for the numbers 1:20
First I created this function:
Find_Remains = function(number, divisor){
Remainder = number%%divisor
Quotient = number%/%divisor
a = c(number, divisor, Remainder, Quotient)
return(a)
}
I add a blank table
final.data.frame = data.frame(matrix(NA, nrow = 1, ncol = 4))
Then I created this for loop
for(i in c(1:20)){
b[i] = Find_Remains(i,5)
final.data.frame = rbind(b[i])
}
the error I get is:
number of items to replace is not a multiple of replacement length
any help would be appreciated. Thanks
r dataframe for-loop division
I am playing with some basic R functions and am trying to create a table that includes the Remainder and Quotient for the numbers 1:20
First I created this function:
Find_Remains = function(number, divisor){
Remainder = number%%divisor
Quotient = number%/%divisor
a = c(number, divisor, Remainder, Quotient)
return(a)
}
I add a blank table
final.data.frame = data.frame(matrix(NA, nrow = 1, ncol = 4))
Then I created this for loop
for(i in c(1:20)){
b[i] = Find_Remains(i,5)
final.data.frame = rbind(b[i])
}
the error I get is:
number of items to replace is not a multiple of replacement length
any help would be appreciated. Thanks
r dataframe for-loop division
r dataframe for-loop division
asked Nov 19 '18 at 22:09
KatieMKatieM
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Kudos on initializing final.data.frame
(although initializing it with 20 rows would have been better I think). Having done that you don't need to use rbind
, you can simply replace each row of the dataframe in every loop run. Here's what you need -
Find_Remains = function(number, divisor) {
Remainder = number%%divisor
Quotient = number%/%divisor
a = c(number, divisor, Remainder, Quotient)
return(a)
}
final.data.frame = data.frame(matrix(NA, nrow = 20, ncol = 4))
for(i in 1:20) {
final.data.frame[i, ] = Find_Remains(i,5)
}
> final.data.frame
X1 X2 X3 X4
1 1 5 1 0
2 2 5 2 0
3 3 5 3 0
4 4 5 4 0
5 5 5 0 1
6 6 5 1 1
7 7 5 2 1
8 8 5 3 1
9 9 5 4 1
10 10 5 0 2
11 11 5 1 2
12 12 5 2 2
13 13 5 3 2
14 14 5 4 2
15 15 5 0 3
16 16 5 1 3
17 17 5 2 3
18 18 5 3 3
19 19 5 4 3
20 20 5 0 4
thanks! That worked! I tried the code with final.data.frame[i] but that didn't work. the code final.data.frame[i,] allows it to be placed in a row.
– KatieM
Nov 19 '18 at 22:56
Happy to help! If this answers your question then consider marking it correct using the tick mark at top left.
– Shree
Nov 19 '18 at 22:57
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383358%2fadding-rows-to-data-table-using-for-loop-in-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
Kudos on initializing final.data.frame
(although initializing it with 20 rows would have been better I think). Having done that you don't need to use rbind
, you can simply replace each row of the dataframe in every loop run. Here's what you need -
Find_Remains = function(number, divisor) {
Remainder = number%%divisor
Quotient = number%/%divisor
a = c(number, divisor, Remainder, Quotient)
return(a)
}
final.data.frame = data.frame(matrix(NA, nrow = 20, ncol = 4))
for(i in 1:20) {
final.data.frame[i, ] = Find_Remains(i,5)
}
> final.data.frame
X1 X2 X3 X4
1 1 5 1 0
2 2 5 2 0
3 3 5 3 0
4 4 5 4 0
5 5 5 0 1
6 6 5 1 1
7 7 5 2 1
8 8 5 3 1
9 9 5 4 1
10 10 5 0 2
11 11 5 1 2
12 12 5 2 2
13 13 5 3 2
14 14 5 4 2
15 15 5 0 3
16 16 5 1 3
17 17 5 2 3
18 18 5 3 3
19 19 5 4 3
20 20 5 0 4
thanks! That worked! I tried the code with final.data.frame[i] but that didn't work. the code final.data.frame[i,] allows it to be placed in a row.
– KatieM
Nov 19 '18 at 22:56
Happy to help! If this answers your question then consider marking it correct using the tick mark at top left.
– Shree
Nov 19 '18 at 22:57
add a comment |
Kudos on initializing final.data.frame
(although initializing it with 20 rows would have been better I think). Having done that you don't need to use rbind
, you can simply replace each row of the dataframe in every loop run. Here's what you need -
Find_Remains = function(number, divisor) {
Remainder = number%%divisor
Quotient = number%/%divisor
a = c(number, divisor, Remainder, Quotient)
return(a)
}
final.data.frame = data.frame(matrix(NA, nrow = 20, ncol = 4))
for(i in 1:20) {
final.data.frame[i, ] = Find_Remains(i,5)
}
> final.data.frame
X1 X2 X3 X4
1 1 5 1 0
2 2 5 2 0
3 3 5 3 0
4 4 5 4 0
5 5 5 0 1
6 6 5 1 1
7 7 5 2 1
8 8 5 3 1
9 9 5 4 1
10 10 5 0 2
11 11 5 1 2
12 12 5 2 2
13 13 5 3 2
14 14 5 4 2
15 15 5 0 3
16 16 5 1 3
17 17 5 2 3
18 18 5 3 3
19 19 5 4 3
20 20 5 0 4
thanks! That worked! I tried the code with final.data.frame[i] but that didn't work. the code final.data.frame[i,] allows it to be placed in a row.
– KatieM
Nov 19 '18 at 22:56
Happy to help! If this answers your question then consider marking it correct using the tick mark at top left.
– Shree
Nov 19 '18 at 22:57
add a comment |
Kudos on initializing final.data.frame
(although initializing it with 20 rows would have been better I think). Having done that you don't need to use rbind
, you can simply replace each row of the dataframe in every loop run. Here's what you need -
Find_Remains = function(number, divisor) {
Remainder = number%%divisor
Quotient = number%/%divisor
a = c(number, divisor, Remainder, Quotient)
return(a)
}
final.data.frame = data.frame(matrix(NA, nrow = 20, ncol = 4))
for(i in 1:20) {
final.data.frame[i, ] = Find_Remains(i,5)
}
> final.data.frame
X1 X2 X3 X4
1 1 5 1 0
2 2 5 2 0
3 3 5 3 0
4 4 5 4 0
5 5 5 0 1
6 6 5 1 1
7 7 5 2 1
8 8 5 3 1
9 9 5 4 1
10 10 5 0 2
11 11 5 1 2
12 12 5 2 2
13 13 5 3 2
14 14 5 4 2
15 15 5 0 3
16 16 5 1 3
17 17 5 2 3
18 18 5 3 3
19 19 5 4 3
20 20 5 0 4
Kudos on initializing final.data.frame
(although initializing it with 20 rows would have been better I think). Having done that you don't need to use rbind
, you can simply replace each row of the dataframe in every loop run. Here's what you need -
Find_Remains = function(number, divisor) {
Remainder = number%%divisor
Quotient = number%/%divisor
a = c(number, divisor, Remainder, Quotient)
return(a)
}
final.data.frame = data.frame(matrix(NA, nrow = 20, ncol = 4))
for(i in 1:20) {
final.data.frame[i, ] = Find_Remains(i,5)
}
> final.data.frame
X1 X2 X3 X4
1 1 5 1 0
2 2 5 2 0
3 3 5 3 0
4 4 5 4 0
5 5 5 0 1
6 6 5 1 1
7 7 5 2 1
8 8 5 3 1
9 9 5 4 1
10 10 5 0 2
11 11 5 1 2
12 12 5 2 2
13 13 5 3 2
14 14 5 4 2
15 15 5 0 3
16 16 5 1 3
17 17 5 2 3
18 18 5 3 3
19 19 5 4 3
20 20 5 0 4
edited Nov 19 '18 at 22:30
answered Nov 19 '18 at 22:23
ShreeShree
3,4111323
3,4111323
thanks! That worked! I tried the code with final.data.frame[i] but that didn't work. the code final.data.frame[i,] allows it to be placed in a row.
– KatieM
Nov 19 '18 at 22:56
Happy to help! If this answers your question then consider marking it correct using the tick mark at top left.
– Shree
Nov 19 '18 at 22:57
add a comment |
thanks! That worked! I tried the code with final.data.frame[i] but that didn't work. the code final.data.frame[i,] allows it to be placed in a row.
– KatieM
Nov 19 '18 at 22:56
Happy to help! If this answers your question then consider marking it correct using the tick mark at top left.
– Shree
Nov 19 '18 at 22:57
thanks! That worked! I tried the code with final.data.frame[i] but that didn't work. the code final.data.frame[i,] allows it to be placed in a row.
– KatieM
Nov 19 '18 at 22:56
thanks! That worked! I tried the code with final.data.frame[i] but that didn't work. the code final.data.frame[i,] allows it to be placed in a row.
– KatieM
Nov 19 '18 at 22:56
Happy to help! If this answers your question then consider marking it correct using the tick mark at top left.
– Shree
Nov 19 '18 at 22:57
Happy to help! If this answers your question then consider marking it correct using the tick mark at top left.
– Shree
Nov 19 '18 at 22:57
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383358%2fadding-rows-to-data-table-using-for-loop-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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