how i can apply which.min function conditionally
I want to create a column in dataframe and populate minimum value in the data frame in column A condition is met :
**Sample data**
Variable value
A 10
A 20
A 30
B 5
B 60
B 70
Expected Result :
Variable value NewColumn
A 10 min
A 20 between min to 20
A 30 between 21 to 30
B 5 min
B 60 between 50 to 60
Is there any easy way to achieve this ?
r
|
show 3 more comments
I want to create a column in dataframe and populate minimum value in the data frame in column A condition is met :
**Sample data**
Variable value
A 10
A 20
A 30
B 5
B 60
B 70
Expected Result :
Variable value NewColumn
A 10 min
A 20 between min to 20
A 30 between 21 to 30
B 5 min
B 60 between 50 to 60
Is there any easy way to achieve this ?
r
what isbetween 20 to 30?
– Sotos
Nov 15 '18 at 12:40
1
It will be easy to spot and flag theminper group, but I'm not sure I get the philosophy you apply to the rest of the values. Can you explain a bit more?
– AntoniosK
Nov 15 '18 at 12:40
I am trying to create a new column and distinguish it in dataframe based on following criterias : a. if value is min based b. if value is between 20 and min, if value between 20 to 30 and so on
– gurtej
Nov 15 '18 at 12:42
So, you want to get min if it is the min value of a factor, and between which two Tens is the value. Is it that ?
– Santiago I. Hurtado
Nov 15 '18 at 13:07
yes thats correct
– gurtej
Nov 15 '18 at 13:13
|
show 3 more comments
I want to create a column in dataframe and populate minimum value in the data frame in column A condition is met :
**Sample data**
Variable value
A 10
A 20
A 30
B 5
B 60
B 70
Expected Result :
Variable value NewColumn
A 10 min
A 20 between min to 20
A 30 between 21 to 30
B 5 min
B 60 between 50 to 60
Is there any easy way to achieve this ?
r
I want to create a column in dataframe and populate minimum value in the data frame in column A condition is met :
**Sample data**
Variable value
A 10
A 20
A 30
B 5
B 60
B 70
Expected Result :
Variable value NewColumn
A 10 min
A 20 between min to 20
A 30 between 21 to 30
B 5 min
B 60 between 50 to 60
Is there any easy way to achieve this ?
r
r
edited Nov 15 '18 at 12:46
gurtej
asked Nov 15 '18 at 12:37
gurtejgurtej
84
84
what isbetween 20 to 30?
– Sotos
Nov 15 '18 at 12:40
1
It will be easy to spot and flag theminper group, but I'm not sure I get the philosophy you apply to the rest of the values. Can you explain a bit more?
– AntoniosK
Nov 15 '18 at 12:40
I am trying to create a new column and distinguish it in dataframe based on following criterias : a. if value is min based b. if value is between 20 and min, if value between 20 to 30 and so on
– gurtej
Nov 15 '18 at 12:42
So, you want to get min if it is the min value of a factor, and between which two Tens is the value. Is it that ?
– Santiago I. Hurtado
Nov 15 '18 at 13:07
yes thats correct
– gurtej
Nov 15 '18 at 13:13
|
show 3 more comments
what isbetween 20 to 30?
– Sotos
Nov 15 '18 at 12:40
1
It will be easy to spot and flag theminper group, but I'm not sure I get the philosophy you apply to the rest of the values. Can you explain a bit more?
– AntoniosK
Nov 15 '18 at 12:40
I am trying to create a new column and distinguish it in dataframe based on following criterias : a. if value is min based b. if value is between 20 and min, if value between 20 to 30 and so on
– gurtej
Nov 15 '18 at 12:42
So, you want to get min if it is the min value of a factor, and between which two Tens is the value. Is it that ?
– Santiago I. Hurtado
Nov 15 '18 at 13:07
yes thats correct
– gurtej
Nov 15 '18 at 13:13
what is
between 20 to 30?– Sotos
Nov 15 '18 at 12:40
what is
between 20 to 30?– Sotos
Nov 15 '18 at 12:40
1
1
It will be easy to spot and flag the
min per group, but I'm not sure I get the philosophy you apply to the rest of the values. Can you explain a bit more?– AntoniosK
Nov 15 '18 at 12:40
It will be easy to spot and flag the
min per group, but I'm not sure I get the philosophy you apply to the rest of the values. Can you explain a bit more?– AntoniosK
Nov 15 '18 at 12:40
I am trying to create a new column and distinguish it in dataframe based on following criterias : a. if value is min based b. if value is between 20 and min, if value between 20 to 30 and so on
– gurtej
Nov 15 '18 at 12:42
I am trying to create a new column and distinguish it in dataframe based on following criterias : a. if value is min based b. if value is between 20 and min, if value between 20 to 30 and so on
– gurtej
Nov 15 '18 at 12:42
So, you want to get min if it is the min value of a factor, and between which two Tens is the value. Is it that ?
– Santiago I. Hurtado
Nov 15 '18 at 13:07
So, you want to get min if it is the min value of a factor, and between which two Tens is the value. Is it that ?
– Santiago I. Hurtado
Nov 15 '18 at 13:07
yes thats correct
– gurtej
Nov 15 '18 at 13:13
yes thats correct
– gurtej
Nov 15 '18 at 13:13
|
show 3 more comments
1 Answer
1
active
oldest
votes
The output in the question seems inconsistent since sometimes the lower bound is 10 less than the upper bound and other times is 9 less. We have defined it to be 9 less and if min is 9 or 10 below the upper bound write min instead of the lower bound.
Now, define a function which inputs a vector x and categorizes it using ave to apply it separately to each group. No packages are used.
categorize <- function(x) upper-10 == min(x), "min", upper-9)
ifelse(x == min(x), "min", paste("between", lower, "to", upper))
transform(DF, New = ave(value, Variable, FUN = categorize))
giving:
Variable value New
1 A 10 min
2 A 20 between min to 20
3 A 30 between 21 to 30
4 B 5 min
5 B 60 between 51 to 60
6 B 70 between 61 to 70
Note
The input DF used, in reproducible form is:
Lines <- "
Variable value
A 10
A 20
A 30
B 5
B 60
B 70"
DF <- read.table(text = Lines, header = TRUE, strip.white = TRUE)
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%2f53319685%2fhow-i-can-apply-which-min-function-conditionally%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
The output in the question seems inconsistent since sometimes the lower bound is 10 less than the upper bound and other times is 9 less. We have defined it to be 9 less and if min is 9 or 10 below the upper bound write min instead of the lower bound.
Now, define a function which inputs a vector x and categorizes it using ave to apply it separately to each group. No packages are used.
categorize <- function(x) upper-10 == min(x), "min", upper-9)
ifelse(x == min(x), "min", paste("between", lower, "to", upper))
transform(DF, New = ave(value, Variable, FUN = categorize))
giving:
Variable value New
1 A 10 min
2 A 20 between min to 20
3 A 30 between 21 to 30
4 B 5 min
5 B 60 between 51 to 60
6 B 70 between 61 to 70
Note
The input DF used, in reproducible form is:
Lines <- "
Variable value
A 10
A 20
A 30
B 5
B 60
B 70"
DF <- read.table(text = Lines, header = TRUE, strip.white = TRUE)
add a comment |
The output in the question seems inconsistent since sometimes the lower bound is 10 less than the upper bound and other times is 9 less. We have defined it to be 9 less and if min is 9 or 10 below the upper bound write min instead of the lower bound.
Now, define a function which inputs a vector x and categorizes it using ave to apply it separately to each group. No packages are used.
categorize <- function(x) upper-10 == min(x), "min", upper-9)
ifelse(x == min(x), "min", paste("between", lower, "to", upper))
transform(DF, New = ave(value, Variable, FUN = categorize))
giving:
Variable value New
1 A 10 min
2 A 20 between min to 20
3 A 30 between 21 to 30
4 B 5 min
5 B 60 between 51 to 60
6 B 70 between 61 to 70
Note
The input DF used, in reproducible form is:
Lines <- "
Variable value
A 10
A 20
A 30
B 5
B 60
B 70"
DF <- read.table(text = Lines, header = TRUE, strip.white = TRUE)
add a comment |
The output in the question seems inconsistent since sometimes the lower bound is 10 less than the upper bound and other times is 9 less. We have defined it to be 9 less and if min is 9 or 10 below the upper bound write min instead of the lower bound.
Now, define a function which inputs a vector x and categorizes it using ave to apply it separately to each group. No packages are used.
categorize <- function(x) upper-10 == min(x), "min", upper-9)
ifelse(x == min(x), "min", paste("between", lower, "to", upper))
transform(DF, New = ave(value, Variable, FUN = categorize))
giving:
Variable value New
1 A 10 min
2 A 20 between min to 20
3 A 30 between 21 to 30
4 B 5 min
5 B 60 between 51 to 60
6 B 70 between 61 to 70
Note
The input DF used, in reproducible form is:
Lines <- "
Variable value
A 10
A 20
A 30
B 5
B 60
B 70"
DF <- read.table(text = Lines, header = TRUE, strip.white = TRUE)
The output in the question seems inconsistent since sometimes the lower bound is 10 less than the upper bound and other times is 9 less. We have defined it to be 9 less and if min is 9 or 10 below the upper bound write min instead of the lower bound.
Now, define a function which inputs a vector x and categorizes it using ave to apply it separately to each group. No packages are used.
categorize <- function(x) upper-10 == min(x), "min", upper-9)
ifelse(x == min(x), "min", paste("between", lower, "to", upper))
transform(DF, New = ave(value, Variable, FUN = categorize))
giving:
Variable value New
1 A 10 min
2 A 20 between min to 20
3 A 30 between 21 to 30
4 B 5 min
5 B 60 between 51 to 60
6 B 70 between 61 to 70
Note
The input DF used, in reproducible form is:
Lines <- "
Variable value
A 10
A 20
A 30
B 5
B 60
B 70"
DF <- read.table(text = Lines, header = TRUE, strip.white = TRUE)
answered Nov 15 '18 at 13:06
G. GrothendieckG. Grothendieck
150k10133238
150k10133238
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%2f53319685%2fhow-i-can-apply-which-min-function-conditionally%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
what is
between 20 to 30?– Sotos
Nov 15 '18 at 12:40
1
It will be easy to spot and flag the
minper group, but I'm not sure I get the philosophy you apply to the rest of the values. Can you explain a bit more?– AntoniosK
Nov 15 '18 at 12:40
I am trying to create a new column and distinguish it in dataframe based on following criterias : a. if value is min based b. if value is between 20 and min, if value between 20 to 30 and so on
– gurtej
Nov 15 '18 at 12:42
So, you want to get min if it is the min value of a factor, and between which two Tens is the value. Is it that ?
– Santiago I. Hurtado
Nov 15 '18 at 13:07
yes thats correct
– gurtej
Nov 15 '18 at 13:13