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,030 points
54 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
• 51,830 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,290 points
83 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,750 points
306 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,820 points
1,298 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
349 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
275 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
115 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
165 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
• 51,830 points
60 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
• 51,830 points
47 views