mathematically manipulate specific elements of vector/object in R
I'm wondering how I can subtract a specific value from elements in a vector that are greater than a threshold I set?
for example, if my data is defined as:
numbers = 1:500
data= sample(numbers)
I now have a random list of numbers between 1 and 500.
I now want to subtract 360 from each value in this vector that is greater than 200. Logically i want to write a for loop with an if statement to do this. I have gone as far to write code that looks like this:
for (i in 1:length(data))
if data[i]>200
data - 360
else
data - 0
This clearly does not work but I am stumped as to what I can do to achieve my goal. Since I will need to plot these data afterwards, I need them to stay in their original order within the output vector. Thanks a ton for the help!
r if-statement vector subtract
add a comment |
I'm wondering how I can subtract a specific value from elements in a vector that are greater than a threshold I set?
for example, if my data is defined as:
numbers = 1:500
data= sample(numbers)
I now have a random list of numbers between 1 and 500.
I now want to subtract 360 from each value in this vector that is greater than 200. Logically i want to write a for loop with an if statement to do this. I have gone as far to write code that looks like this:
for (i in 1:length(data))
if data[i]>200
data - 360
else
data - 0
This clearly does not work but I am stumped as to what I can do to achieve my goal. Since I will need to plot these data afterwards, I need them to stay in their original order within the output vector. Thanks a ton for the help!
r if-statement vector subtract
add a comment |
I'm wondering how I can subtract a specific value from elements in a vector that are greater than a threshold I set?
for example, if my data is defined as:
numbers = 1:500
data= sample(numbers)
I now have a random list of numbers between 1 and 500.
I now want to subtract 360 from each value in this vector that is greater than 200. Logically i want to write a for loop with an if statement to do this. I have gone as far to write code that looks like this:
for (i in 1:length(data))
if data[i]>200
data - 360
else
data - 0
This clearly does not work but I am stumped as to what I can do to achieve my goal. Since I will need to plot these data afterwards, I need them to stay in their original order within the output vector. Thanks a ton for the help!
r if-statement vector subtract
I'm wondering how I can subtract a specific value from elements in a vector that are greater than a threshold I set?
for example, if my data is defined as:
numbers = 1:500
data= sample(numbers)
I now have a random list of numbers between 1 and 500.
I now want to subtract 360 from each value in this vector that is greater than 200. Logically i want to write a for loop with an if statement to do this. I have gone as far to write code that looks like this:
for (i in 1:length(data))
if data[i]>200
data - 360
else
data - 0
This clearly does not work but I am stumped as to what I can do to achieve my goal. Since I will need to plot these data afterwards, I need them to stay in their original order within the output vector. Thanks a ton for the help!
r if-statement vector subtract
r if-statement vector subtract
asked Nov 13 '18 at 1:23
user1554925user1554925
203
203
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
ifelse()
is perfect for the purpose, you can use the data vector of your sample:
data <- ifelse( data > 200, data - 360, data )
So merely to give another taste:
set.seed( 1110 ) # make it reproducible
data <- sample( numbers )
head( data, 10 )
[1] 242 395 440 287 110 46 241 489 276 178
data[ data > 200 ] <- data[ data > 200 ] - 360 # replace inline
head( data )
[1] -118 35 80 -73 110 46 -119 129 -84 178
Your loop would have worked as well after correcting some mistakes, see below:
for (i in 1:length(data))
if( data[i]>200 )
data[ i ] <- data[ i ] - 360
THANK YOU SO MUCH!!! The first one works PERFECTLY! Good to know about such a simple, useful function.
– user1554925
Nov 13 '18 at 20:42
add a comment |
Well a simple answer is this
x[x>200] = x[x>200] - 360
x>200
: return a logical vector were each value thas is greater than 200 is TRUE and other FALSE- Use the logical vector to access the element of your initial vector. Assign or access only the element that has a TRUE value in the logical vector
Your code is wrong because you are using wrong the operator [. It must throw an error.
for (i in 1:length(data))
if (data[i]>200)
data[i] = data[i] - 360
This is the correct way. You must read R from the start to understand better the operators...
add a comment |
There is no need to use loops. Here's the simplest way -
data <- data - 360*(data > 200)
Demo -
set.seed(1)
numbers <- 1:500
data <- sample(numbers)
head(data)
# [1] 133 186 286 452 101 445
data <- data - 360*(data > 200)
head(data)
# [1] 133 186 -74 92 101 85
add a comment |
I would use ifelse
numbers = 1:500
data= sample(numbers)
new_numbers <- ifelse(numbers >200,numbers-360, numbers)
new_numbers
He want to substract only the numbers that are greater than 200 and all the others to be the same...
– Csd
Nov 13 '18 at 1:41
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%2f53272432%2fmathematically-manipulate-specific-elements-of-vector-object-in-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
ifelse()
is perfect for the purpose, you can use the data vector of your sample:
data <- ifelse( data > 200, data - 360, data )
So merely to give another taste:
set.seed( 1110 ) # make it reproducible
data <- sample( numbers )
head( data, 10 )
[1] 242 395 440 287 110 46 241 489 276 178
data[ data > 200 ] <- data[ data > 200 ] - 360 # replace inline
head( data )
[1] -118 35 80 -73 110 46 -119 129 -84 178
Your loop would have worked as well after correcting some mistakes, see below:
for (i in 1:length(data))
if( data[i]>200 )
data[ i ] <- data[ i ] - 360
THANK YOU SO MUCH!!! The first one works PERFECTLY! Good to know about such a simple, useful function.
– user1554925
Nov 13 '18 at 20:42
add a comment |
ifelse()
is perfect for the purpose, you can use the data vector of your sample:
data <- ifelse( data > 200, data - 360, data )
So merely to give another taste:
set.seed( 1110 ) # make it reproducible
data <- sample( numbers )
head( data, 10 )
[1] 242 395 440 287 110 46 241 489 276 178
data[ data > 200 ] <- data[ data > 200 ] - 360 # replace inline
head( data )
[1] -118 35 80 -73 110 46 -119 129 -84 178
Your loop would have worked as well after correcting some mistakes, see below:
for (i in 1:length(data))
if( data[i]>200 )
data[ i ] <- data[ i ] - 360
THANK YOU SO MUCH!!! The first one works PERFECTLY! Good to know about such a simple, useful function.
– user1554925
Nov 13 '18 at 20:42
add a comment |
ifelse()
is perfect for the purpose, you can use the data vector of your sample:
data <- ifelse( data > 200, data - 360, data )
So merely to give another taste:
set.seed( 1110 ) # make it reproducible
data <- sample( numbers )
head( data, 10 )
[1] 242 395 440 287 110 46 241 489 276 178
data[ data > 200 ] <- data[ data > 200 ] - 360 # replace inline
head( data )
[1] -118 35 80 -73 110 46 -119 129 -84 178
Your loop would have worked as well after correcting some mistakes, see below:
for (i in 1:length(data))
if( data[i]>200 )
data[ i ] <- data[ i ] - 360
ifelse()
is perfect for the purpose, you can use the data vector of your sample:
data <- ifelse( data > 200, data - 360, data )
So merely to give another taste:
set.seed( 1110 ) # make it reproducible
data <- sample( numbers )
head( data, 10 )
[1] 242 395 440 287 110 46 241 489 276 178
data[ data > 200 ] <- data[ data > 200 ] - 360 # replace inline
head( data )
[1] -118 35 80 -73 110 46 -119 129 -84 178
Your loop would have worked as well after correcting some mistakes, see below:
for (i in 1:length(data))
if( data[i]>200 )
data[ i ] <- data[ i ] - 360
edited Nov 13 '18 at 1:54
answered Nov 13 '18 at 1:47
vaettchenvaettchen
5,2901332
5,2901332
THANK YOU SO MUCH!!! The first one works PERFECTLY! Good to know about such a simple, useful function.
– user1554925
Nov 13 '18 at 20:42
add a comment |
THANK YOU SO MUCH!!! The first one works PERFECTLY! Good to know about such a simple, useful function.
– user1554925
Nov 13 '18 at 20:42
THANK YOU SO MUCH!!! The first one works PERFECTLY! Good to know about such a simple, useful function.
– user1554925
Nov 13 '18 at 20:42
THANK YOU SO MUCH!!! The first one works PERFECTLY! Good to know about such a simple, useful function.
– user1554925
Nov 13 '18 at 20:42
add a comment |
Well a simple answer is this
x[x>200] = x[x>200] - 360
x>200
: return a logical vector were each value thas is greater than 200 is TRUE and other FALSE- Use the logical vector to access the element of your initial vector. Assign or access only the element that has a TRUE value in the logical vector
Your code is wrong because you are using wrong the operator [. It must throw an error.
for (i in 1:length(data))
if (data[i]>200)
data[i] = data[i] - 360
This is the correct way. You must read R from the start to understand better the operators...
add a comment |
Well a simple answer is this
x[x>200] = x[x>200] - 360
x>200
: return a logical vector were each value thas is greater than 200 is TRUE and other FALSE- Use the logical vector to access the element of your initial vector. Assign or access only the element that has a TRUE value in the logical vector
Your code is wrong because you are using wrong the operator [. It must throw an error.
for (i in 1:length(data))
if (data[i]>200)
data[i] = data[i] - 360
This is the correct way. You must read R from the start to understand better the operators...
add a comment |
Well a simple answer is this
x[x>200] = x[x>200] - 360
x>200
: return a logical vector were each value thas is greater than 200 is TRUE and other FALSE- Use the logical vector to access the element of your initial vector. Assign or access only the element that has a TRUE value in the logical vector
Your code is wrong because you are using wrong the operator [. It must throw an error.
for (i in 1:length(data))
if (data[i]>200)
data[i] = data[i] - 360
This is the correct way. You must read R from the start to understand better the operators...
Well a simple answer is this
x[x>200] = x[x>200] - 360
x>200
: return a logical vector were each value thas is greater than 200 is TRUE and other FALSE- Use the logical vector to access the element of your initial vector. Assign or access only the element that has a TRUE value in the logical vector
Your code is wrong because you are using wrong the operator [. It must throw an error.
for (i in 1:length(data))
if (data[i]>200)
data[i] = data[i] - 360
This is the correct way. You must read R from the start to understand better the operators...
answered Nov 13 '18 at 1:32
CsdCsd
31819
31819
add a comment |
add a comment |
There is no need to use loops. Here's the simplest way -
data <- data - 360*(data > 200)
Demo -
set.seed(1)
numbers <- 1:500
data <- sample(numbers)
head(data)
# [1] 133 186 286 452 101 445
data <- data - 360*(data > 200)
head(data)
# [1] 133 186 -74 92 101 85
add a comment |
There is no need to use loops. Here's the simplest way -
data <- data - 360*(data > 200)
Demo -
set.seed(1)
numbers <- 1:500
data <- sample(numbers)
head(data)
# [1] 133 186 286 452 101 445
data <- data - 360*(data > 200)
head(data)
# [1] 133 186 -74 92 101 85
add a comment |
There is no need to use loops. Here's the simplest way -
data <- data - 360*(data > 200)
Demo -
set.seed(1)
numbers <- 1:500
data <- sample(numbers)
head(data)
# [1] 133 186 286 452 101 445
data <- data - 360*(data > 200)
head(data)
# [1] 133 186 -74 92 101 85
There is no need to use loops. Here's the simplest way -
data <- data - 360*(data > 200)
Demo -
set.seed(1)
numbers <- 1:500
data <- sample(numbers)
head(data)
# [1] 133 186 286 452 101 445
data <- data - 360*(data > 200)
head(data)
# [1] 133 186 -74 92 101 85
answered Nov 13 '18 at 2:20
ShreeShree
3,4911424
3,4911424
add a comment |
add a comment |
I would use ifelse
numbers = 1:500
data= sample(numbers)
new_numbers <- ifelse(numbers >200,numbers-360, numbers)
new_numbers
He want to substract only the numbers that are greater than 200 and all the others to be the same...
– Csd
Nov 13 '18 at 1:41
add a comment |
I would use ifelse
numbers = 1:500
data= sample(numbers)
new_numbers <- ifelse(numbers >200,numbers-360, numbers)
new_numbers
He want to substract only the numbers that are greater than 200 and all the others to be the same...
– Csd
Nov 13 '18 at 1:41
add a comment |
I would use ifelse
numbers = 1:500
data= sample(numbers)
new_numbers <- ifelse(numbers >200,numbers-360, numbers)
new_numbers
I would use ifelse
numbers = 1:500
data= sample(numbers)
new_numbers <- ifelse(numbers >200,numbers-360, numbers)
new_numbers
answered Nov 13 '18 at 1:25
Harro CyrankaHarro Cyranka
1,6181615
1,6181615
He want to substract only the numbers that are greater than 200 and all the others to be the same...
– Csd
Nov 13 '18 at 1:41
add a comment |
He want to substract only the numbers that are greater than 200 and all the others to be the same...
– Csd
Nov 13 '18 at 1:41
He want to substract only the numbers that are greater than 200 and all the others to be the same...
– Csd
Nov 13 '18 at 1:41
He want to substract only the numbers that are greater than 200 and all the others to be the same...
– Csd
Nov 13 '18 at 1:41
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%2f53272432%2fmathematically-manipulate-specific-elements-of-vector-object-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