0

Merging disparate data frames

 5 months ago
source link: https://www.codesd.com/item/merging-disparate-data-frames.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Merging disparate data frames

advertisements

So this issue involves merging two data frames of disparate length..

example code:

library(ggplot2)
p_i = data.frame(date=sample(1:40,20,replace=F),index_1 = runif(20,0,1),index_2 = runif(20,0,2))

p_i<-p_i[with(p_i,order(date)),]
w_d = data.frame(date=c(1:40),C=runif(40,2,38))
y_w <- seq(0, 40, length = 9)
x_w <- c(unique(p_i$date))

p_w <- ggplot(w_d, aes(x = date, y = C))+
    geom_bin2d(breaks = list(x = x_w, y = y_w))+
    scale_fill_continuous(low = "#99CCFF", high = "#CC0000", space = "Lab", na.value = "grey50", guide = "colourbar")

w_dat <- ggplot_build(p_w)$data[[1]]

What i'm trying to do is get the counts from "w_dat" merged into the "p_i" data frame, organized by date so I can regress either of the indices in p_i against the count in a given range, each range being a unique column.


Sorry, I confused w_d with w_dat

You can use dplyr for that.

library(dplyr)
mydf <- full_join(p_i, w_d, by = "date")

If you also want to remove the rows with missings, use filter(mydf, !is.na(index_1)).

Tags ggplot2

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK