Hi guys,

I need your help with grouping a variable

I have a continuous variable 'budget' which goes from -6847 (negative) to +81204 (positive)
I'm trying to create the groups using the following code (which does what it should when values are positive)

```
balance_groups <- c(paste(seq(-10000,90000, by=10000), seq(-10000 + 10000 - 1, 100000-1, by=10000),
sep = "-"), paste(100000, "+", sep = ""))
```

unfortunately the output I am getting from this code is the following:

balance_groups
"-10000--1" "0-9999" "10000-19999" "20000-29999" "30000-39999" "40000-49999" "50000-59999" "60000-69999" "70000-79999" "80000-89999" "90000-99999" "1e+05+"

I tried various variations of the numbers, but I must be doing something wrong as the groups are always incorrect. It's mainly the first group which is wrong. I'd like it to be -10,000 - 0, "1-9999". I tried various combinations but none work!

I would be grateful for your help
Thank you

This is not necessarily better than simply doing it by hand.

author: roc
date: 2021-04-05
output: "reprex::reprex\_document"
title: dopey-nyala_reprex.R
```
suppressPackageStartupMessages({
library(dplyr)
})
paste(s, " - ", sep = "")
#> Error in paste(s, " - ", sep = ""): object 's' not found
s <- seq(from = -10000, to = 100000,by = 10000)
evens <- s[which(s %% 2 == 0)]
odds <- (s-1)[which((s-1 %% 2 != 0))]
dat <- data.frame(evens = evens,odds = odds)
dat <- dat %>% mutate(asmb = lead(odds,1),
odds = evens)
dat[1,3] <- 0
dat[2,2] <- 1
dat <- dat %>% mutate(result = paste(odds,asmb,sep = " - "))
dat$result
#> [1] "-10000 - 0" "1 - 9999" "10000 - 19999" "20000 - 29999"
#> [5] "30000 - 39999" "40000 - 49999" "50000 - 59999" "60000 - 69999"
#> [9] "70000 - 79999" "80000 - 89999" "90000 - 99999" "1e+05 - NA"
```

1 Like

Wow, technocrat! You're a wiz! It took me a bit of time to even understand all the steps! It's a long-winded way but worked, so thank you.

1 Like

system
Closed
April 13, 2021, 8:59pm
4
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.