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
• 14,080 points
11 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 35,800 points

Related Questions In Data Analytics

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 darklord
• 6,140 points
21 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 21, 2018 in Data Analytics by zombie
• 3,690 points
74 views
0 votes
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,000 points
205 views
0 votes
1 answer

Can we have an if loop inside a for loop in R programming?

You're If loop doesn't have any condition ...READ MORE

answered Dec 21, 2018 in Data Analytics by Sophie may
• 9,050 points
34 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,700 points
23 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,700 points
62 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,700 points
35 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,700 points
32 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
• 35,800 points
18 views
0 votes
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
• 35,800 points
17 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.