If-else in R code

0 votes

I have data, something like this:

1.586077^+00
3.242631^-03
2.426304^-03
8.616781^-04
5.396826^-03
2.947846^-04
1.018943^+01
8.639457^-03
8.458051^-03
2.807870^+02

now I need a code which goes through these numbers and if the number is greater than 1, then it writes it down. If the number is less than 1, then it adds it till it hits the next number greater than 1 and then writes the sum from the beginning to that point.

Nov 5, 2018 in Data Analytics by Hannah
• 18,540 points
712 views

1 answer to this question.

0 votes
x <- c(.5, 0.2, 2, 3.4, 0.4, 1.2, .2, .8, 2.3, 7.4)
result.final <- NULL
cumul.total <- 0
for (i in x) {
  cumul.total <- cumul.total + i
  if(i > 1){
    result <- cumul.total
  }
  else {
    next
  }
  result.final <- c(result.final, result)
}
result.final

I think this should do your job.

answered Nov 5, 2018 by Kalgi
• 52,350 points

Related Questions In Data Analytics

0 votes
1 answer
0 votes
1 answer

Check if the object(variable) is defined in R

You can use the exists(): > exists("toFindUnknown") [1] FALSE > ...READ MORE

answered Apr 17, 2018 in Data Analytics by Sahiti
• 6,370 points
777 views
0 votes
1 answer

How can I Split code over multiple lines in an R script?

You can do this as follows: setwd(paste("~/a/very/long/path/here ...READ MORE

answered May 22, 2018 in Data Analytics by zombie
• 3,790 points
1,960 views
+1 vote
1 answer

"Error in eval(ei, envir) : object 'RDX2' not found" when trying to source the code in R

This is a very common issue that ...READ MORE

answered Oct 30, 2018 in Data Analytics by Maverick
• 10,840 points
5,072 views
0 votes
1 answer

By using dpylr package sum of multiple columns

Basically here we are making an equation ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
2,218 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,878 views
0 votes
1 answer

In a dpylr pipline how to use sample and seq?

For avoiding rowwise(), I prefer to use ...READ MORE

answered Apr 6, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 9, 2020 by Gitika 1,141 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,284 views
0 votes
1 answer

"Error in if" while trying to execute simple code in R

This caused non-logical data or missing values passed ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 52,350 points
598 views
+1 vote
1 answer

How to check if object is defines in R?

You can use the exists function for ...READ MORE

answered Nov 6, 2018 in Data Analytics by Kalgi
• 52,350 points
700 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP