I want to create a new dataframe with two columns, each column from a seperate dataframe, and then plot onto the same bar chart
Both columns are called OKS_score.
df1 is preopOKS, this has 645 rows and df2 is postopOKS, this has 643 rows
I have tried left_join
df3 <- preopOKS$OKS_score %>%
left_join(postopOKS$OKS_score, copy = TRUE)
I have tried to assign each column and rbind
them: however this makes a DF with 2 rows of mulitple values.
x <- preopOKS$OKS_score
y <- postopOKS$OKS_score
z <- rbind(x,y)
str(z)
num [1:2, 1:645] 36 52 20 34 20 31 33 45 29 51 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:2] "x" "y"
..$ : NULL
I have even tried to pivot_longer to make the df into a format I can plot with:
z %>%
pivot_longer(names_to = 'x', values_to = 'y')
As you can tell, I'm very new and can't even get my head around where I'm going wrong, with my methods, or my terribly basic code.
CodePudding user response:
Here's a solution, using the name "OKS" for the original data frame (which is used to generate "preopOKS" and "postopOKS") and assuming it has an "OKS_score" column:
library("dplyr")
# add unique id column to original data frame:
OKS$ID <- 1:nrow(OKS)
# assuming preop data frame has unchanged scores, select just score and ID,
# but rename score so it's different from postOKS score name:
preopOKS <- OKS[, c("ID", "OKS_score_pre")]
# make postop data frame with whatever function is used to change score:
postopOKS <- data.frame(ID = OKS$ID, OKS_score_post = whatever_function(OKS$OKS_score))
# recombine them
finalOKS <- left_join(preopOKS, postopOKS, by = "ID")
It would be easier, depending on your use case, to just keep the two scores in the same data frame from the start:
library("tidyverse")
OKS <- OKS %>%
mutate(OKS_score_pre = OKS_score,
OKS_score_post = whatever_function(OKS_score)) %>%
select(OKS_score_pre, OKS_score_post)
CodePudding user response:
Consider 2 columns:
cola<-runif(20,0,50)
colb<-runif(30,0,50)
Now we want equal column sizes. For that we can attribute the length of the column with maximum size to the other column. Then:
length(cola)<-length(colb)
Now,lets create a new dataframe. For simplicity lets first create a dataframe and name for each column, and combine the two single dataframe columns into one dataframe of two columns:
cola<-data.frame(a=cola)
colb<-data.frame(b=colb)
df<-data.frame(cola,colb)
Then to quickly plot as barplot side by side you can:
par(mfrow=c(1,2))
barplot(df$a,legend.text = names(df[1]))
barplot(df$b,col="blue",beside=TRUE, legend.text = names(df[2]))
But using ggpplot you can do a better barplot.