modify value in each row of a dataframe
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have an input called target and a dataframe:
target <- 3
d <- data.frame(x=c(1,2,3),y=c(4,5,6))
x y
1 4
2 5
3 6
I want for each row:
if the value of column x < target then this value <- 0
if the value of column y > target then this value <- 9
the result:
x y
0 9
0 9
3 9
how could I get this?
I tried to use the function apply
but it didn't allow to modify the value in dataframe d
.
r
add a comment |
I have an input called target and a dataframe:
target <- 3
d <- data.frame(x=c(1,2,3),y=c(4,5,6))
x y
1 4
2 5
3 6
I want for each row:
if the value of column x < target then this value <- 0
if the value of column y > target then this value <- 9
the result:
x y
0 9
0 9
3 9
how could I get this?
I tried to use the function apply
but it didn't allow to modify the value in dataframe d
.
r
I think you have a typo in the third row of your expectedx
output.
– Tim Biegeleisen
Jan 4 at 13:35
add a comment |
I have an input called target and a dataframe:
target <- 3
d <- data.frame(x=c(1,2,3),y=c(4,5,6))
x y
1 4
2 5
3 6
I want for each row:
if the value of column x < target then this value <- 0
if the value of column y > target then this value <- 9
the result:
x y
0 9
0 9
3 9
how could I get this?
I tried to use the function apply
but it didn't allow to modify the value in dataframe d
.
r
I have an input called target and a dataframe:
target <- 3
d <- data.frame(x=c(1,2,3),y=c(4,5,6))
x y
1 4
2 5
3 6
I want for each row:
if the value of column x < target then this value <- 0
if the value of column y > target then this value <- 9
the result:
x y
0 9
0 9
3 9
how could I get this?
I tried to use the function apply
but it didn't allow to modify the value in dataframe d
.
r
r
edited Jan 4 at 13:44
Ronak Shah
48k104269
48k104269
asked Jan 4 at 13:31
DD chenDD chen
217
217
I think you have a typo in the third row of your expectedx
output.
– Tim Biegeleisen
Jan 4 at 13:35
add a comment |
I think you have a typo in the third row of your expectedx
output.
– Tim Biegeleisen
Jan 4 at 13:35
I think you have a typo in the third row of your expected
x
output.– Tim Biegeleisen
Jan 4 at 13:35
I think you have a typo in the third row of your expected
x
output.– Tim Biegeleisen
Jan 4 at 13:35
add a comment |
3 Answers
3
active
oldest
votes
With dplyr and case_when:
library(dplyr)
d <- d %<%
mutate(target = 3,
x = case_when(x < target ~ 0,
T ~ x),
y = case_when(y > target ~ 9,
T ~ y))
With base r:
d$x[d$x < target,] <- 0
d$y[d$y > target,] <- 9
thank you for your response, i have a second question, if the target is a vector (same length as nrow(d) ). that means the target is different for each row. do the function mutate still work?
– DD chen
Jan 4 at 13:48
If you have a vector containing all of the targets e.g.targets <- c(1, 2, 3)
then yes you can mutate this onto your data frame and then use case_when to compare with the targets column and change your x/y columns accordingly.
– Tom Haddow
Jan 4 at 13:52
Essentially mutate will compare row by row but for this to work your target vector needs to be a column in your data frame. If you already have your target as a vector the dplyr example I posted should work except you would change the linemutate(target = 3,
to bemutate(target = target,
so that your target vector is now the "target" column of your data frame.
– Tom Haddow
Jan 4 at 13:58
1
thanks a lot , i try with base r and that works! and if i want the target become the value that i want affect to (if the condition is true), i just run: d$y[d$y > target,] <- target[d$y > target,]
– DD chen
Jan 4 at 14:20
add a comment |
Multiple ways to do this. One way using double replace
replace(replace(d, d > target, 9), d < target, 0)
# x y
#1 0 9
#2 0 9
#3 3 9
This logic can also be used in dplyr
chain
library(dplyr)
d %>% mutate_all(funs(replace(replace(., . > target, 9), . < target, 0)))
thank you for your response, but if the target is also a vector (same length as nrow(d). which means the target is different for each row. do the function mutate still work?
– DD chen
Jan 4 at 13:46
@DDchen nope, If you havetarget
as vector I don't thinkmutate
would work as it is as shown in the answer butreplace
would still work. Please check it on your real data.
– Ronak Shah
Jan 4 at 13:53
thank you @Ronak Shah !
– DD chen
Jan 4 at 14:22
@DDchen you are welcome. Just FYI, you can select only one answer as accepted.
– Ronak Shah
Jan 4 at 14:26
add a comment |
A straighforward way only using R base.
# Add object.
target <- 3
# Create dataframe.
df1 <- data.frame(x = c(1, 2, 3),y=c(4, 5, 6))
# Copy dataframe [df1] to new dataframe [ðf2].
# Preserve old dataframe [df1] for comparison.
df2 <- df1
# Insert digits.
df2$x[df2$x < target] <- 0
df2$y[df2$y > target] <- 9
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%2f54039933%2fmodify-value-in-each-row-of-a-dataframe%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
With dplyr and case_when:
library(dplyr)
d <- d %<%
mutate(target = 3,
x = case_when(x < target ~ 0,
T ~ x),
y = case_when(y > target ~ 9,
T ~ y))
With base r:
d$x[d$x < target,] <- 0
d$y[d$y > target,] <- 9
thank you for your response, i have a second question, if the target is a vector (same length as nrow(d) ). that means the target is different for each row. do the function mutate still work?
– DD chen
Jan 4 at 13:48
If you have a vector containing all of the targets e.g.targets <- c(1, 2, 3)
then yes you can mutate this onto your data frame and then use case_when to compare with the targets column and change your x/y columns accordingly.
– Tom Haddow
Jan 4 at 13:52
Essentially mutate will compare row by row but for this to work your target vector needs to be a column in your data frame. If you already have your target as a vector the dplyr example I posted should work except you would change the linemutate(target = 3,
to bemutate(target = target,
so that your target vector is now the "target" column of your data frame.
– Tom Haddow
Jan 4 at 13:58
1
thanks a lot , i try with base r and that works! and if i want the target become the value that i want affect to (if the condition is true), i just run: d$y[d$y > target,] <- target[d$y > target,]
– DD chen
Jan 4 at 14:20
add a comment |
With dplyr and case_when:
library(dplyr)
d <- d %<%
mutate(target = 3,
x = case_when(x < target ~ 0,
T ~ x),
y = case_when(y > target ~ 9,
T ~ y))
With base r:
d$x[d$x < target,] <- 0
d$y[d$y > target,] <- 9
thank you for your response, i have a second question, if the target is a vector (same length as nrow(d) ). that means the target is different for each row. do the function mutate still work?
– DD chen
Jan 4 at 13:48
If you have a vector containing all of the targets e.g.targets <- c(1, 2, 3)
then yes you can mutate this onto your data frame and then use case_when to compare with the targets column and change your x/y columns accordingly.
– Tom Haddow
Jan 4 at 13:52
Essentially mutate will compare row by row but for this to work your target vector needs to be a column in your data frame. If you already have your target as a vector the dplyr example I posted should work except you would change the linemutate(target = 3,
to bemutate(target = target,
so that your target vector is now the "target" column of your data frame.
– Tom Haddow
Jan 4 at 13:58
1
thanks a lot , i try with base r and that works! and if i want the target become the value that i want affect to (if the condition is true), i just run: d$y[d$y > target,] <- target[d$y > target,]
– DD chen
Jan 4 at 14:20
add a comment |
With dplyr and case_when:
library(dplyr)
d <- d %<%
mutate(target = 3,
x = case_when(x < target ~ 0,
T ~ x),
y = case_when(y > target ~ 9,
T ~ y))
With base r:
d$x[d$x < target,] <- 0
d$y[d$y > target,] <- 9
With dplyr and case_when:
library(dplyr)
d <- d %<%
mutate(target = 3,
x = case_when(x < target ~ 0,
T ~ x),
y = case_when(y > target ~ 9,
T ~ y))
With base r:
d$x[d$x < target,] <- 0
d$y[d$y > target,] <- 9
edited Jan 4 at 13:42
answered Jan 4 at 13:37
Tom HaddowTom Haddow
17510
17510
thank you for your response, i have a second question, if the target is a vector (same length as nrow(d) ). that means the target is different for each row. do the function mutate still work?
– DD chen
Jan 4 at 13:48
If you have a vector containing all of the targets e.g.targets <- c(1, 2, 3)
then yes you can mutate this onto your data frame and then use case_when to compare with the targets column and change your x/y columns accordingly.
– Tom Haddow
Jan 4 at 13:52
Essentially mutate will compare row by row but for this to work your target vector needs to be a column in your data frame. If you already have your target as a vector the dplyr example I posted should work except you would change the linemutate(target = 3,
to bemutate(target = target,
so that your target vector is now the "target" column of your data frame.
– Tom Haddow
Jan 4 at 13:58
1
thanks a lot , i try with base r and that works! and if i want the target become the value that i want affect to (if the condition is true), i just run: d$y[d$y > target,] <- target[d$y > target,]
– DD chen
Jan 4 at 14:20
add a comment |
thank you for your response, i have a second question, if the target is a vector (same length as nrow(d) ). that means the target is different for each row. do the function mutate still work?
– DD chen
Jan 4 at 13:48
If you have a vector containing all of the targets e.g.targets <- c(1, 2, 3)
then yes you can mutate this onto your data frame and then use case_when to compare with the targets column and change your x/y columns accordingly.
– Tom Haddow
Jan 4 at 13:52
Essentially mutate will compare row by row but for this to work your target vector needs to be a column in your data frame. If you already have your target as a vector the dplyr example I posted should work except you would change the linemutate(target = 3,
to bemutate(target = target,
so that your target vector is now the "target" column of your data frame.
– Tom Haddow
Jan 4 at 13:58
1
thanks a lot , i try with base r and that works! and if i want the target become the value that i want affect to (if the condition is true), i just run: d$y[d$y > target,] <- target[d$y > target,]
– DD chen
Jan 4 at 14:20
thank you for your response, i have a second question, if the target is a vector (same length as nrow(d) ). that means the target is different for each row. do the function mutate still work?
– DD chen
Jan 4 at 13:48
thank you for your response, i have a second question, if the target is a vector (same length as nrow(d) ). that means the target is different for each row. do the function mutate still work?
– DD chen
Jan 4 at 13:48
If you have a vector containing all of the targets e.g.
targets <- c(1, 2, 3)
then yes you can mutate this onto your data frame and then use case_when to compare with the targets column and change your x/y columns accordingly.– Tom Haddow
Jan 4 at 13:52
If you have a vector containing all of the targets e.g.
targets <- c(1, 2, 3)
then yes you can mutate this onto your data frame and then use case_when to compare with the targets column and change your x/y columns accordingly.– Tom Haddow
Jan 4 at 13:52
Essentially mutate will compare row by row but for this to work your target vector needs to be a column in your data frame. If you already have your target as a vector the dplyr example I posted should work except you would change the line
mutate(target = 3,
to be mutate(target = target,
so that your target vector is now the "target" column of your data frame.– Tom Haddow
Jan 4 at 13:58
Essentially mutate will compare row by row but for this to work your target vector needs to be a column in your data frame. If you already have your target as a vector the dplyr example I posted should work except you would change the line
mutate(target = 3,
to be mutate(target = target,
so that your target vector is now the "target" column of your data frame.– Tom Haddow
Jan 4 at 13:58
1
1
thanks a lot , i try with base r and that works! and if i want the target become the value that i want affect to (if the condition is true), i just run: d$y[d$y > target,] <- target[d$y > target,]
– DD chen
Jan 4 at 14:20
thanks a lot , i try with base r and that works! and if i want the target become the value that i want affect to (if the condition is true), i just run: d$y[d$y > target,] <- target[d$y > target,]
– DD chen
Jan 4 at 14:20
add a comment |
Multiple ways to do this. One way using double replace
replace(replace(d, d > target, 9), d < target, 0)
# x y
#1 0 9
#2 0 9
#3 3 9
This logic can also be used in dplyr
chain
library(dplyr)
d %>% mutate_all(funs(replace(replace(., . > target, 9), . < target, 0)))
thank you for your response, but if the target is also a vector (same length as nrow(d). which means the target is different for each row. do the function mutate still work?
– DD chen
Jan 4 at 13:46
@DDchen nope, If you havetarget
as vector I don't thinkmutate
would work as it is as shown in the answer butreplace
would still work. Please check it on your real data.
– Ronak Shah
Jan 4 at 13:53
thank you @Ronak Shah !
– DD chen
Jan 4 at 14:22
@DDchen you are welcome. Just FYI, you can select only one answer as accepted.
– Ronak Shah
Jan 4 at 14:26
add a comment |
Multiple ways to do this. One way using double replace
replace(replace(d, d > target, 9), d < target, 0)
# x y
#1 0 9
#2 0 9
#3 3 9
This logic can also be used in dplyr
chain
library(dplyr)
d %>% mutate_all(funs(replace(replace(., . > target, 9), . < target, 0)))
thank you for your response, but if the target is also a vector (same length as nrow(d). which means the target is different for each row. do the function mutate still work?
– DD chen
Jan 4 at 13:46
@DDchen nope, If you havetarget
as vector I don't thinkmutate
would work as it is as shown in the answer butreplace
would still work. Please check it on your real data.
– Ronak Shah
Jan 4 at 13:53
thank you @Ronak Shah !
– DD chen
Jan 4 at 14:22
@DDchen you are welcome. Just FYI, you can select only one answer as accepted.
– Ronak Shah
Jan 4 at 14:26
add a comment |
Multiple ways to do this. One way using double replace
replace(replace(d, d > target, 9), d < target, 0)
# x y
#1 0 9
#2 0 9
#3 3 9
This logic can also be used in dplyr
chain
library(dplyr)
d %>% mutate_all(funs(replace(replace(., . > target, 9), . < target, 0)))
Multiple ways to do this. One way using double replace
replace(replace(d, d > target, 9), d < target, 0)
# x y
#1 0 9
#2 0 9
#3 3 9
This logic can also be used in dplyr
chain
library(dplyr)
d %>% mutate_all(funs(replace(replace(., . > target, 9), . < target, 0)))
answered Jan 4 at 13:35
Ronak ShahRonak Shah
48k104269
48k104269
thank you for your response, but if the target is also a vector (same length as nrow(d). which means the target is different for each row. do the function mutate still work?
– DD chen
Jan 4 at 13:46
@DDchen nope, If you havetarget
as vector I don't thinkmutate
would work as it is as shown in the answer butreplace
would still work. Please check it on your real data.
– Ronak Shah
Jan 4 at 13:53
thank you @Ronak Shah !
– DD chen
Jan 4 at 14:22
@DDchen you are welcome. Just FYI, you can select only one answer as accepted.
– Ronak Shah
Jan 4 at 14:26
add a comment |
thank you for your response, but if the target is also a vector (same length as nrow(d). which means the target is different for each row. do the function mutate still work?
– DD chen
Jan 4 at 13:46
@DDchen nope, If you havetarget
as vector I don't thinkmutate
would work as it is as shown in the answer butreplace
would still work. Please check it on your real data.
– Ronak Shah
Jan 4 at 13:53
thank you @Ronak Shah !
– DD chen
Jan 4 at 14:22
@DDchen you are welcome. Just FYI, you can select only one answer as accepted.
– Ronak Shah
Jan 4 at 14:26
thank you for your response, but if the target is also a vector (same length as nrow(d). which means the target is different for each row. do the function mutate still work?
– DD chen
Jan 4 at 13:46
thank you for your response, but if the target is also a vector (same length as nrow(d). which means the target is different for each row. do the function mutate still work?
– DD chen
Jan 4 at 13:46
@DDchen nope, If you have
target
as vector I don't think mutate
would work as it is as shown in the answer but replace
would still work. Please check it on your real data.– Ronak Shah
Jan 4 at 13:53
@DDchen nope, If you have
target
as vector I don't think mutate
would work as it is as shown in the answer but replace
would still work. Please check it on your real data.– Ronak Shah
Jan 4 at 13:53
thank you @Ronak Shah !
– DD chen
Jan 4 at 14:22
thank you @Ronak Shah !
– DD chen
Jan 4 at 14:22
@DDchen you are welcome. Just FYI, you can select only one answer as accepted.
– Ronak Shah
Jan 4 at 14:26
@DDchen you are welcome. Just FYI, you can select only one answer as accepted.
– Ronak Shah
Jan 4 at 14:26
add a comment |
A straighforward way only using R base.
# Add object.
target <- 3
# Create dataframe.
df1 <- data.frame(x = c(1, 2, 3),y=c(4, 5, 6))
# Copy dataframe [df1] to new dataframe [ðf2].
# Preserve old dataframe [df1] for comparison.
df2 <- df1
# Insert digits.
df2$x[df2$x < target] <- 0
df2$y[df2$y > target] <- 9
add a comment |
A straighforward way only using R base.
# Add object.
target <- 3
# Create dataframe.
df1 <- data.frame(x = c(1, 2, 3),y=c(4, 5, 6))
# Copy dataframe [df1] to new dataframe [ðf2].
# Preserve old dataframe [df1] for comparison.
df2 <- df1
# Insert digits.
df2$x[df2$x < target] <- 0
df2$y[df2$y > target] <- 9
add a comment |
A straighforward way only using R base.
# Add object.
target <- 3
# Create dataframe.
df1 <- data.frame(x = c(1, 2, 3),y=c(4, 5, 6))
# Copy dataframe [df1] to new dataframe [ðf2].
# Preserve old dataframe [df1] for comparison.
df2 <- df1
# Insert digits.
df2$x[df2$x < target] <- 0
df2$y[df2$y > target] <- 9
A straighforward way only using R base.
# Add object.
target <- 3
# Create dataframe.
df1 <- data.frame(x = c(1, 2, 3),y=c(4, 5, 6))
# Copy dataframe [df1] to new dataframe [ðf2].
# Preserve old dataframe [df1] for comparison.
df2 <- df1
# Insert digits.
df2$x[df2$x < target] <- 0
df2$y[df2$y > target] <- 9
answered Jan 5 at 15:27
ToolboxToolbox
672312
672312
add a comment |
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%2f54039933%2fmodify-value-in-each-row-of-a-dataframe%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
I think you have a typo in the third row of your expected
x
output.– Tim Biegeleisen
Jan 4 at 13:35