Home > front end >  Binning two columns of data frame together in R
Binning two columns of data frame together in R

Time:09-08

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!

CodePudding user response:

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.

CodePudding user response:

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()
  • Related