User defined function in R is not working
I am trying to run below code in R but it's not working. Need your help. Let me explain my requirement.
I have data frame named Data. In that I have column called view. Let's consider view contains 6 values 1,2,3,5,20,NA.
Now I want to calculate 99 percentile on view. Using quantile function on this data will give me quantile value 19 (rounded off).
Now I am checking condition if view column having any value greater than 19, replace it with 19. Here in our example, we have one value greater than 19 which is 20, so 20 should be replace with 19.
Now, if I run all the code manually like below it works fine
Data <- data.frame(view=c(1,2,3,5,20,NA))
summary(Data)
n = quantile(Data$view, 0.99, na.rm = TRUE)
Data$view[Data$view > n] <- n
But I want to create a function which can just call variable from data frame and do the above job. Below is the function I tried, but not working.
ReplaceQuantile <- function(x) {
n = quantile(na.omit(x),0.99)
x[x > n] <- n
}
ReplaceQuantile (Data$view)
Hope now my requirement is clear.
r
add a comment |
I am trying to run below code in R but it's not working. Need your help. Let me explain my requirement.
I have data frame named Data. In that I have column called view. Let's consider view contains 6 values 1,2,3,5,20,NA.
Now I want to calculate 99 percentile on view. Using quantile function on this data will give me quantile value 19 (rounded off).
Now I am checking condition if view column having any value greater than 19, replace it with 19. Here in our example, we have one value greater than 19 which is 20, so 20 should be replace with 19.
Now, if I run all the code manually like below it works fine
Data <- data.frame(view=c(1,2,3,5,20,NA))
summary(Data)
n = quantile(Data$view, 0.99, na.rm = TRUE)
Data$view[Data$view > n] <- n
But I want to create a function which can just call variable from data frame and do the above job. Below is the function I tried, but not working.
ReplaceQuantile <- function(x) {
n = quantile(na.omit(x),0.99)
x[x > n] <- n
}
ReplaceQuantile (Data$view)
Hope now my requirement is clear.
r
1
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Jaap
Nov 25 '18 at 9:13
Do you just want to do this for selected columns or all?
– Joe
Nov 25 '18 at 9:26
add a comment |
I am trying to run below code in R but it's not working. Need your help. Let me explain my requirement.
I have data frame named Data. In that I have column called view. Let's consider view contains 6 values 1,2,3,5,20,NA.
Now I want to calculate 99 percentile on view. Using quantile function on this data will give me quantile value 19 (rounded off).
Now I am checking condition if view column having any value greater than 19, replace it with 19. Here in our example, we have one value greater than 19 which is 20, so 20 should be replace with 19.
Now, if I run all the code manually like below it works fine
Data <- data.frame(view=c(1,2,3,5,20,NA))
summary(Data)
n = quantile(Data$view, 0.99, na.rm = TRUE)
Data$view[Data$view > n] <- n
But I want to create a function which can just call variable from data frame and do the above job. Below is the function I tried, but not working.
ReplaceQuantile <- function(x) {
n = quantile(na.omit(x),0.99)
x[x > n] <- n
}
ReplaceQuantile (Data$view)
Hope now my requirement is clear.
r
I am trying to run below code in R but it's not working. Need your help. Let me explain my requirement.
I have data frame named Data. In that I have column called view. Let's consider view contains 6 values 1,2,3,5,20,NA.
Now I want to calculate 99 percentile on view. Using quantile function on this data will give me quantile value 19 (rounded off).
Now I am checking condition if view column having any value greater than 19, replace it with 19. Here in our example, we have one value greater than 19 which is 20, so 20 should be replace with 19.
Now, if I run all the code manually like below it works fine
Data <- data.frame(view=c(1,2,3,5,20,NA))
summary(Data)
n = quantile(Data$view, 0.99, na.rm = TRUE)
Data$view[Data$view > n] <- n
But I want to create a function which can just call variable from data frame and do the above job. Below is the function I tried, but not working.
ReplaceQuantile <- function(x) {
n = quantile(na.omit(x),0.99)
x[x > n] <- n
}
ReplaceQuantile (Data$view)
Hope now my requirement is clear.
r
r
edited Nov 25 '18 at 10:36
AkselA
4,57421325
4,57421325
asked Nov 25 '18 at 9:11
kushal chawdakushal chawda
133
133
1
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Jaap
Nov 25 '18 at 9:13
Do you just want to do this for selected columns or all?
– Joe
Nov 25 '18 at 9:26
add a comment |
1
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Jaap
Nov 25 '18 at 9:13
Do you just want to do this for selected columns or all?
– Joe
Nov 25 '18 at 9:26
1
1
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Jaap
Nov 25 '18 at 9:13
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Jaap
Nov 25 '18 at 9:13
Do you just want to do this for selected columns or all?
– Joe
Nov 25 '18 at 9:26
Do you just want to do this for selected columns or all?
– Joe
Nov 25 '18 at 9:26
add a comment |
1 Answer
1
active
oldest
votes
Your code is almost working. I recommend to use na.rm = TRUE in your quantile function instead of na.omit:
ReplaceQuantile <- function(x) {
n = quantile(x, 0.99, na.rm = TRUE)
x[x > n] <- n
x
}
ReplaceQuantile (Data$view)
This way you remove NA and your function should run.
EDIT: Consider this reproducible example:
Data <- data.frame(view = c(seq(1, 5, by = .1), NA))
ReplaceQuantile <- function(x) {
n = quantile(x, 0.99, na.rm = TRUE)
x[x > n] <- n
x
}
ReplaceQuantile(Data$view)
EDIT II for clarification: You need to assign the result to Data$view in order to overwrite the original values:
Data <- data.frame(view=c(1,2,3,5,20,NA))
ReplaceQuantile <- function(x) {
n = quantile(x, 0.99, na.rm = TRUE)
x[x > n] <- n
x
}
Data$view <- ReplaceQuantile(Data$view)
This way you should get the following results:
Data$view
[1] 1.0 2.0 3.0 5.0 19.4 NA
Hi, I did the same as suggested but later if I check summary of Data$view, seems values are not getting replaced with quantile value in data frame
– kushal chawda
Nov 25 '18 at 9:39
You are right! My bad but are you replacing the result of your function intoData$viewlike this:Data$view <- ReplaceQuantile(Data$view)
– Ben Nutzer
Nov 25 '18 at 9:40
1
@BenNutzer You have to return the value ofx. I have edited the function with justxas the last code line. Oh, upvoted thena.rmidea, it's much better thanna.omit.
– Rui Barradas
Nov 25 '18 at 9:41
@kushalchawda See if it works now.
– Rui Barradas
Nov 25 '18 at 9:43
@RuiBarradas Good point. Thanks for editing.
– Ben Nutzer
Nov 25 '18 at 9:43
|
show 8 more comments
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%2f53466089%2fuser-defined-function-in-r-is-not-working%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
Your code is almost working. I recommend to use na.rm = TRUE in your quantile function instead of na.omit:
ReplaceQuantile <- function(x) {
n = quantile(x, 0.99, na.rm = TRUE)
x[x > n] <- n
x
}
ReplaceQuantile (Data$view)
This way you remove NA and your function should run.
EDIT: Consider this reproducible example:
Data <- data.frame(view = c(seq(1, 5, by = .1), NA))
ReplaceQuantile <- function(x) {
n = quantile(x, 0.99, na.rm = TRUE)
x[x > n] <- n
x
}
ReplaceQuantile(Data$view)
EDIT II for clarification: You need to assign the result to Data$view in order to overwrite the original values:
Data <- data.frame(view=c(1,2,3,5,20,NA))
ReplaceQuantile <- function(x) {
n = quantile(x, 0.99, na.rm = TRUE)
x[x > n] <- n
x
}
Data$view <- ReplaceQuantile(Data$view)
This way you should get the following results:
Data$view
[1] 1.0 2.0 3.0 5.0 19.4 NA
Hi, I did the same as suggested but later if I check summary of Data$view, seems values are not getting replaced with quantile value in data frame
– kushal chawda
Nov 25 '18 at 9:39
You are right! My bad but are you replacing the result of your function intoData$viewlike this:Data$view <- ReplaceQuantile(Data$view)
– Ben Nutzer
Nov 25 '18 at 9:40
1
@BenNutzer You have to return the value ofx. I have edited the function with justxas the last code line. Oh, upvoted thena.rmidea, it's much better thanna.omit.
– Rui Barradas
Nov 25 '18 at 9:41
@kushalchawda See if it works now.
– Rui Barradas
Nov 25 '18 at 9:43
@RuiBarradas Good point. Thanks for editing.
– Ben Nutzer
Nov 25 '18 at 9:43
|
show 8 more comments
Your code is almost working. I recommend to use na.rm = TRUE in your quantile function instead of na.omit:
ReplaceQuantile <- function(x) {
n = quantile(x, 0.99, na.rm = TRUE)
x[x > n] <- n
x
}
ReplaceQuantile (Data$view)
This way you remove NA and your function should run.
EDIT: Consider this reproducible example:
Data <- data.frame(view = c(seq(1, 5, by = .1), NA))
ReplaceQuantile <- function(x) {
n = quantile(x, 0.99, na.rm = TRUE)
x[x > n] <- n
x
}
ReplaceQuantile(Data$view)
EDIT II for clarification: You need to assign the result to Data$view in order to overwrite the original values:
Data <- data.frame(view=c(1,2,3,5,20,NA))
ReplaceQuantile <- function(x) {
n = quantile(x, 0.99, na.rm = TRUE)
x[x > n] <- n
x
}
Data$view <- ReplaceQuantile(Data$view)
This way you should get the following results:
Data$view
[1] 1.0 2.0 3.0 5.0 19.4 NA
Hi, I did the same as suggested but later if I check summary of Data$view, seems values are not getting replaced with quantile value in data frame
– kushal chawda
Nov 25 '18 at 9:39
You are right! My bad but are you replacing the result of your function intoData$viewlike this:Data$view <- ReplaceQuantile(Data$view)
– Ben Nutzer
Nov 25 '18 at 9:40
1
@BenNutzer You have to return the value ofx. I have edited the function with justxas the last code line. Oh, upvoted thena.rmidea, it's much better thanna.omit.
– Rui Barradas
Nov 25 '18 at 9:41
@kushalchawda See if it works now.
– Rui Barradas
Nov 25 '18 at 9:43
@RuiBarradas Good point. Thanks for editing.
– Ben Nutzer
Nov 25 '18 at 9:43
|
show 8 more comments
Your code is almost working. I recommend to use na.rm = TRUE in your quantile function instead of na.omit:
ReplaceQuantile <- function(x) {
n = quantile(x, 0.99, na.rm = TRUE)
x[x > n] <- n
x
}
ReplaceQuantile (Data$view)
This way you remove NA and your function should run.
EDIT: Consider this reproducible example:
Data <- data.frame(view = c(seq(1, 5, by = .1), NA))
ReplaceQuantile <- function(x) {
n = quantile(x, 0.99, na.rm = TRUE)
x[x > n] <- n
x
}
ReplaceQuantile(Data$view)
EDIT II for clarification: You need to assign the result to Data$view in order to overwrite the original values:
Data <- data.frame(view=c(1,2,3,5,20,NA))
ReplaceQuantile <- function(x) {
n = quantile(x, 0.99, na.rm = TRUE)
x[x > n] <- n
x
}
Data$view <- ReplaceQuantile(Data$view)
This way you should get the following results:
Data$view
[1] 1.0 2.0 3.0 5.0 19.4 NA
Your code is almost working. I recommend to use na.rm = TRUE in your quantile function instead of na.omit:
ReplaceQuantile <- function(x) {
n = quantile(x, 0.99, na.rm = TRUE)
x[x > n] <- n
x
}
ReplaceQuantile (Data$view)
This way you remove NA and your function should run.
EDIT: Consider this reproducible example:
Data <- data.frame(view = c(seq(1, 5, by = .1), NA))
ReplaceQuantile <- function(x) {
n = quantile(x, 0.99, na.rm = TRUE)
x[x > n] <- n
x
}
ReplaceQuantile(Data$view)
EDIT II for clarification: You need to assign the result to Data$view in order to overwrite the original values:
Data <- data.frame(view=c(1,2,3,5,20,NA))
ReplaceQuantile <- function(x) {
n = quantile(x, 0.99, na.rm = TRUE)
x[x > n] <- n
x
}
Data$view <- ReplaceQuantile(Data$view)
This way you should get the following results:
Data$view
[1] 1.0 2.0 3.0 5.0 19.4 NA
edited Nov 25 '18 at 10:48
answered Nov 25 '18 at 9:28
Ben NutzerBen Nutzer
9818
9818
Hi, I did the same as suggested but later if I check summary of Data$view, seems values are not getting replaced with quantile value in data frame
– kushal chawda
Nov 25 '18 at 9:39
You are right! My bad but are you replacing the result of your function intoData$viewlike this:Data$view <- ReplaceQuantile(Data$view)
– Ben Nutzer
Nov 25 '18 at 9:40
1
@BenNutzer You have to return the value ofx. I have edited the function with justxas the last code line. Oh, upvoted thena.rmidea, it's much better thanna.omit.
– Rui Barradas
Nov 25 '18 at 9:41
@kushalchawda See if it works now.
– Rui Barradas
Nov 25 '18 at 9:43
@RuiBarradas Good point. Thanks for editing.
– Ben Nutzer
Nov 25 '18 at 9:43
|
show 8 more comments
Hi, I did the same as suggested but later if I check summary of Data$view, seems values are not getting replaced with quantile value in data frame
– kushal chawda
Nov 25 '18 at 9:39
You are right! My bad but are you replacing the result of your function intoData$viewlike this:Data$view <- ReplaceQuantile(Data$view)
– Ben Nutzer
Nov 25 '18 at 9:40
1
@BenNutzer You have to return the value ofx. I have edited the function with justxas the last code line. Oh, upvoted thena.rmidea, it's much better thanna.omit.
– Rui Barradas
Nov 25 '18 at 9:41
@kushalchawda See if it works now.
– Rui Barradas
Nov 25 '18 at 9:43
@RuiBarradas Good point. Thanks for editing.
– Ben Nutzer
Nov 25 '18 at 9:43
Hi, I did the same as suggested but later if I check summary of Data$view, seems values are not getting replaced with quantile value in data frame
– kushal chawda
Nov 25 '18 at 9:39
Hi, I did the same as suggested but later if I check summary of Data$view, seems values are not getting replaced with quantile value in data frame
– kushal chawda
Nov 25 '18 at 9:39
You are right! My bad but are you replacing the result of your function into
Data$view like this: Data$view <- ReplaceQuantile(Data$view)– Ben Nutzer
Nov 25 '18 at 9:40
You are right! My bad but are you replacing the result of your function into
Data$view like this: Data$view <- ReplaceQuantile(Data$view)– Ben Nutzer
Nov 25 '18 at 9:40
1
1
@BenNutzer You have to return the value of
x. I have edited the function with just x as the last code line. Oh, upvoted the na.rm idea, it's much better than na.omit.– Rui Barradas
Nov 25 '18 at 9:41
@BenNutzer You have to return the value of
x. I have edited the function with just x as the last code line. Oh, upvoted the na.rm idea, it's much better than na.omit.– Rui Barradas
Nov 25 '18 at 9:41
@kushalchawda See if it works now.
– Rui Barradas
Nov 25 '18 at 9:43
@kushalchawda See if it works now.
– Rui Barradas
Nov 25 '18 at 9:43
@RuiBarradas Good point. Thanks for editing.
– Ben Nutzer
Nov 25 '18 at 9:43
@RuiBarradas Good point. Thanks for editing.
– Ben Nutzer
Nov 25 '18 at 9:43
|
show 8 more comments
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%2f53466089%2fuser-defined-function-in-r-is-not-working%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
1
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Jaap
Nov 25 '18 at 9:13
Do you just want to do this for selected columns or all?
– Joe
Nov 25 '18 at 9:26