I would like to format a variable in R, using round, floor or ceiling. However, I would like to sometimes use floor, sometimes ceiling for different values of the same variable. Is that possible?
My dataframe is data and the variable I want to format is var. These are its values (with frequencies):
Value    |      Freq.
---------|-----------
1        |       1504
1.333333 |        397
1.5      |          9
1.666667 |        612
2        |       2096
2.333333 |       1057
2.5      |         18
2.666667 |       1270
3        |       2913
3.333333 |       1487
3.5      |         35
3.666667 |       1374
4        |       2007
4.333333 |        779
4.5      |         16
4.666667 |        522
5        |       1913
NaN      |        553
My desired result is a variable var2 that looks like this:
Value |      Freq.
------|-----------
1     |       1910
2     |       3783
3     |       5670
4     |       4195
5     |       2451     
NaN   |        553
So, 1.5 and 2.5 are adjusted downward (floor), but 3.5 and 4.5 are adjusted upward (ceiling). The other values are rounded the usual way.
My attempt is this, but it does not work yet:
data$var2 <- format(round(data$var, 1))
if (data$var2 == 1.7||2.7||3.5||3.7||4.5||4.7) {
  data$var2 <- format(ceiling(data$var2))
} else {
  data$var2 <- format(floor(data$var2))
}
I know that there are probably several mistakes in my attempt and would appreciate any help.
PS: What I'm actually looking for is an equivalent for Stata's function egen cut. With that it is very easy to achieve the desired result:
egen var2 = cut(var), at(1, 1.6, 2.6, 3.5, 4.4, 5.1)
recode var2 (1 = 1) (1.6 = 2) (2.6 = 3) (3.5 = 4) (4.4 = 5)