In R: How to create a column such that there is an initial value, and each next value is previous plus a set...
I apologize if this is a repetitive question to others listed... I have looked and looked and tried multiple ideas but can't seem to find a working answer.
I have a column "Expected" within list TFAOS with multiple columns.
edit: dataframe is:
patients <- c(1:17)
Expected <- c(0)
TFAOS <- data.frame(patients, Expected)
Expected should be:
Expected
17/24
previous value + 17/24
previous value + 17/24
etc. for 17 rows
OR THE EQUIVALENT:
Expected
17/24 * 1
17/24 * 2
17/24 * 3
etc. for 17 rows
Can anyone help with a simple solution?
I have tried:
TFAOS[1,2]=17/24
TFAOS[2:17, 2] = TFAOS$Expected[-1] + 17/24
AND
mutate(TFAOS, Expected = 17/24 + lag(Expected, default =0))
These both just give the same 17/24 value for each row.
Thank you so so much in advance.
r list reference
add a comment |
I apologize if this is a repetitive question to others listed... I have looked and looked and tried multiple ideas but can't seem to find a working answer.
I have a column "Expected" within list TFAOS with multiple columns.
edit: dataframe is:
patients <- c(1:17)
Expected <- c(0)
TFAOS <- data.frame(patients, Expected)
Expected should be:
Expected
17/24
previous value + 17/24
previous value + 17/24
etc. for 17 rows
OR THE EQUIVALENT:
Expected
17/24 * 1
17/24 * 2
17/24 * 3
etc. for 17 rows
Can anyone help with a simple solution?
I have tried:
TFAOS[1,2]=17/24
TFAOS[2:17, 2] = TFAOS$Expected[-1] + 17/24
AND
mutate(TFAOS, Expected = 17/24 + lag(Expected, default =0))
These both just give the same 17/24 value for each row.
Thank you so so much in advance.
r list reference
It’s easier to help if you provide the data in a format that people can copy and paste into R as a data set. Also what is 17/24 here? Is it supposed to be character (text) or is it supposed to be numerical?
– DaveM
Jan 3 at 17:20
Thank you for the feedback, I will post in a repeatable format below. 17/24 is a numerical value (0.7083333).
– ClareFG
Jan 3 at 17:25
add a comment |
I apologize if this is a repetitive question to others listed... I have looked and looked and tried multiple ideas but can't seem to find a working answer.
I have a column "Expected" within list TFAOS with multiple columns.
edit: dataframe is:
patients <- c(1:17)
Expected <- c(0)
TFAOS <- data.frame(patients, Expected)
Expected should be:
Expected
17/24
previous value + 17/24
previous value + 17/24
etc. for 17 rows
OR THE EQUIVALENT:
Expected
17/24 * 1
17/24 * 2
17/24 * 3
etc. for 17 rows
Can anyone help with a simple solution?
I have tried:
TFAOS[1,2]=17/24
TFAOS[2:17, 2] = TFAOS$Expected[-1] + 17/24
AND
mutate(TFAOS, Expected = 17/24 + lag(Expected, default =0))
These both just give the same 17/24 value for each row.
Thank you so so much in advance.
r list reference
I apologize if this is a repetitive question to others listed... I have looked and looked and tried multiple ideas but can't seem to find a working answer.
I have a column "Expected" within list TFAOS with multiple columns.
edit: dataframe is:
patients <- c(1:17)
Expected <- c(0)
TFAOS <- data.frame(patients, Expected)
Expected should be:
Expected
17/24
previous value + 17/24
previous value + 17/24
etc. for 17 rows
OR THE EQUIVALENT:
Expected
17/24 * 1
17/24 * 2
17/24 * 3
etc. for 17 rows
Can anyone help with a simple solution?
I have tried:
TFAOS[1,2]=17/24
TFAOS[2:17, 2] = TFAOS$Expected[-1] + 17/24
AND
mutate(TFAOS, Expected = 17/24 + lag(Expected, default =0))
These both just give the same 17/24 value for each row.
Thank you so so much in advance.
r list reference
r list reference
edited Jan 3 at 17:33
ClareFG
asked Jan 3 at 17:11
ClareFGClareFG
126
126
It’s easier to help if you provide the data in a format that people can copy and paste into R as a data set. Also what is 17/24 here? Is it supposed to be character (text) or is it supposed to be numerical?
– DaveM
Jan 3 at 17:20
Thank you for the feedback, I will post in a repeatable format below. 17/24 is a numerical value (0.7083333).
– ClareFG
Jan 3 at 17:25
add a comment |
It’s easier to help if you provide the data in a format that people can copy and paste into R as a data set. Also what is 17/24 here? Is it supposed to be character (text) or is it supposed to be numerical?
– DaveM
Jan 3 at 17:20
Thank you for the feedback, I will post in a repeatable format below. 17/24 is a numerical value (0.7083333).
– ClareFG
Jan 3 at 17:25
It’s easier to help if you provide the data in a format that people can copy and paste into R as a data set. Also what is 17/24 here? Is it supposed to be character (text) or is it supposed to be numerical?
– DaveM
Jan 3 at 17:20
It’s easier to help if you provide the data in a format that people can copy and paste into R as a data set. Also what is 17/24 here? Is it supposed to be character (text) or is it supposed to be numerical?
– DaveM
Jan 3 at 17:20
Thank you for the feedback, I will post in a repeatable format below. 17/24 is a numerical value (0.7083333).
– ClareFG
Jan 3 at 17:25
Thank you for the feedback, I will post in a repeatable format below. 17/24 is a numerical value (0.7083333).
– ClareFG
Jan 3 at 17:25
add a comment |
3 Answers
3
active
oldest
votes
Here is another way you could do using your data:
# package load
library(tidyverse)
# data
patients <- c(1:17)
Expected <- c(0)
TFAOS <- data.frame(patients, Expected)
# constant to add
myconstant <- 17/24
# mutate
TFAOS <- TFAOS %>%
mutate(Expected = Expected + (patients - 1) * myconstant)
print(TFAOS)
Works perfectly as well! like using mutate as it can be done in one step and more transferable. thank you so much!
– ClareFG
Jan 3 at 18:27
add a comment |
You can try mutate(Expected = 1:n()*17/4 )
add a comment |
Not totally sure I'm answering your question, but this code will take an initial value, a vector length, and a step size and give you a vector which starts at the initial value, and increases by the step size (possibly an integer) in each successive element.
out_length <- 10 # length of the vector you want
increment <- 4 # amount to increment in each step
initial <- 3 # starting value
(0:(out_length - 1))*increment + initial # the desired vector
[1] 3 7 11 15 19 23 27 31 35 39
Thank you very much! this works perfectly on a fundamental level
– ClareFG
Jan 3 at 17:54
add a comment |
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%2f54026810%2fin-r-how-to-create-a-column-such-that-there-is-an-initial-value-and-each-next%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is another way you could do using your data:
# package load
library(tidyverse)
# data
patients <- c(1:17)
Expected <- c(0)
TFAOS <- data.frame(patients, Expected)
# constant to add
myconstant <- 17/24
# mutate
TFAOS <- TFAOS %>%
mutate(Expected = Expected + (patients - 1) * myconstant)
print(TFAOS)
Works perfectly as well! like using mutate as it can be done in one step and more transferable. thank you so much!
– ClareFG
Jan 3 at 18:27
add a comment |
Here is another way you could do using your data:
# package load
library(tidyverse)
# data
patients <- c(1:17)
Expected <- c(0)
TFAOS <- data.frame(patients, Expected)
# constant to add
myconstant <- 17/24
# mutate
TFAOS <- TFAOS %>%
mutate(Expected = Expected + (patients - 1) * myconstant)
print(TFAOS)
Works perfectly as well! like using mutate as it can be done in one step and more transferable. thank you so much!
– ClareFG
Jan 3 at 18:27
add a comment |
Here is another way you could do using your data:
# package load
library(tidyverse)
# data
patients <- c(1:17)
Expected <- c(0)
TFAOS <- data.frame(patients, Expected)
# constant to add
myconstant <- 17/24
# mutate
TFAOS <- TFAOS %>%
mutate(Expected = Expected + (patients - 1) * myconstant)
print(TFAOS)
Here is another way you could do using your data:
# package load
library(tidyverse)
# data
patients <- c(1:17)
Expected <- c(0)
TFAOS <- data.frame(patients, Expected)
# constant to add
myconstant <- 17/24
# mutate
TFAOS <- TFAOS %>%
mutate(Expected = Expected + (patients - 1) * myconstant)
print(TFAOS)
answered Jan 3 at 18:02
DaveMDaveM
1188
1188
Works perfectly as well! like using mutate as it can be done in one step and more transferable. thank you so much!
– ClareFG
Jan 3 at 18:27
add a comment |
Works perfectly as well! like using mutate as it can be done in one step and more transferable. thank you so much!
– ClareFG
Jan 3 at 18:27
Works perfectly as well! like using mutate as it can be done in one step and more transferable. thank you so much!
– ClareFG
Jan 3 at 18:27
Works perfectly as well! like using mutate as it can be done in one step and more transferable. thank you so much!
– ClareFG
Jan 3 at 18:27
add a comment |
You can try mutate(Expected = 1:n()*17/4 )
add a comment |
You can try mutate(Expected = 1:n()*17/4 )
add a comment |
You can try mutate(Expected = 1:n()*17/4 )
You can try mutate(Expected = 1:n()*17/4 )
answered Jan 3 at 17:20
Hugo SilvaHugo Silva
1627
1627
add a comment |
add a comment |
Not totally sure I'm answering your question, but this code will take an initial value, a vector length, and a step size and give you a vector which starts at the initial value, and increases by the step size (possibly an integer) in each successive element.
out_length <- 10 # length of the vector you want
increment <- 4 # amount to increment in each step
initial <- 3 # starting value
(0:(out_length - 1))*increment + initial # the desired vector
[1] 3 7 11 15 19 23 27 31 35 39
Thank you very much! this works perfectly on a fundamental level
– ClareFG
Jan 3 at 17:54
add a comment |
Not totally sure I'm answering your question, but this code will take an initial value, a vector length, and a step size and give you a vector which starts at the initial value, and increases by the step size (possibly an integer) in each successive element.
out_length <- 10 # length of the vector you want
increment <- 4 # amount to increment in each step
initial <- 3 # starting value
(0:(out_length - 1))*increment + initial # the desired vector
[1] 3 7 11 15 19 23 27 31 35 39
Thank you very much! this works perfectly on a fundamental level
– ClareFG
Jan 3 at 17:54
add a comment |
Not totally sure I'm answering your question, but this code will take an initial value, a vector length, and a step size and give you a vector which starts at the initial value, and increases by the step size (possibly an integer) in each successive element.
out_length <- 10 # length of the vector you want
increment <- 4 # amount to increment in each step
initial <- 3 # starting value
(0:(out_length - 1))*increment + initial # the desired vector
[1] 3 7 11 15 19 23 27 31 35 39
Not totally sure I'm answering your question, but this code will take an initial value, a vector length, and a step size and give you a vector which starts at the initial value, and increases by the step size (possibly an integer) in each successive element.
out_length <- 10 # length of the vector you want
increment <- 4 # amount to increment in each step
initial <- 3 # starting value
(0:(out_length - 1))*increment + initial # the desired vector
[1] 3 7 11 15 19 23 27 31 35 39
answered Jan 3 at 17:29
Joseph Clark McIntyreJoseph Clark McIntyre
939126
939126
Thank you very much! this works perfectly on a fundamental level
– ClareFG
Jan 3 at 17:54
add a comment |
Thank you very much! this works perfectly on a fundamental level
– ClareFG
Jan 3 at 17:54
Thank you very much! this works perfectly on a fundamental level
– ClareFG
Jan 3 at 17:54
Thank you very much! this works perfectly on a fundamental level
– ClareFG
Jan 3 at 17:54
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%2f54026810%2fin-r-how-to-create-a-column-such-that-there-is-an-initial-value-and-each-next%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
It’s easier to help if you provide the data in a format that people can copy and paste into R as a data set. Also what is 17/24 here? Is it supposed to be character (text) or is it supposed to be numerical?
– DaveM
Jan 3 at 17:20
Thank you for the feedback, I will post in a repeatable format below. 17/24 is a numerical value (0.7083333).
– ClareFG
Jan 3 at 17:25