-1

I would like to bin two columns of a dataset simultaneously to create one common binned column. The simple code is as follows

x <- sample(100)
y <- sample(100)

data <- data.frame(x, y)

xbin <- seq(from = 0, to = 100, by = 10)
ybin <- seq(from = 0, to = 100, by = 10)

Any help is appreciated!

Harsh Krishna
  • 83
  • 1
  • 4
  • 1
    Can you give an example of how a common binned column should look like? – Bernhard Sep 07 '22 at 12:39
  • Using this code data %>% ggplot() + geom_bin_2d(aes(x = x, y = y), binwidth = c(10,10), colour = "red") + theme_minimal() Every one of the 100 boxes on the plot is one bin. i would like the data to be in the same binned form – Harsh Krishna Sep 07 '22 at 12:43

2 Answers2

1

Not sure if this is what you are looking for

library(tidyverse)

x <- sample(100)
y <- sample(100)

data <- data.frame(x, y)

xbin <- seq(from = 0, to = 100, by = 10)
ybin <- seq(from = 0, to = 100, by = 10)


data <- data%>%
  dplyr::mutate(
    x_binned = cut(x, breaks = seq(0,100,10)),
    y_binned = cut(y, breaks = seq(0,100,10))
  )

data %>% 
  ggplot() +
  geom_bin_2d(
    aes(x = x_binned, y = y_binned), binwidth = c(10,10), colour = "red") +
  theme_minimal()
0

After asking in the comments I am still not quite shure, what the desired answer would look like but I hope, that one of the two answers in the below code will work for you:

x <- sample(100)
y <- sample(100)
data <- data.frame(x, y)
xbin <- seq(from = 0, to = 100, by = 10)
ybin <- seq(from = 0, to = 100, by = 10)

data$xbin <- cut(data$x, breaks = xbin, ordered = TRUE)
data$ybin <- cut(data$y, breaks = ybin, ordered = TRUE)
data$commonbin1 <- paste0(data$xbin, data$ybin)
data$commonbin2 <- paste0("(",as.numeric(data$xbin),";", as.numeric(data$ybin),")")

head(data, 20)

This will construct a common binning variable commonbin1 that includes the bin-limits in the names of the bins and commonbin2 which will be easier to compare to the plot mentioned in the comment.

Bernhard
  • 4,272
  • 1
  • 13
  • 23