1

I have several text files containing 2 columns and different row numbers. I would like to follow drawing a plot using ggplot2 as explained enter link description here; however, it works well for dataframes with equal row numbers, and I couldn't reproduce it with dataframes with different row numbers. please let me know how I should combine these data frames (dataframes with different row number) using R?

case siza
case1 129
case2 129
case3 130 
case4 131
case5 132
case6 132

Thank you

9
  • I assume they're the same columns in each data frame, named the same? If so, just use z <- rbind(x, y) where x and y are your two data frames. Commented Jan 8, 2016 at 21:28
  • dataframe have the same columns but, the different row. For right plotting, they have to combined based on 2th coulmns (named for example "size"), could you please help me how I can do it for these dataframes? I used "df = data.frame(df1$size,df2$size) for dataframes with equal row number, but don't know for dataframes with different row number. Commented Jan 8, 2016 at 21:59
  • are you trying to (1) merge/bind the columns together, then (2) melt and plot them? If so you'll want to cbind the columns then plot them. Commented Jan 9, 2016 at 1:07
  • If you edit your question with some sample data, you will get a clearer answer. Commented Jan 9, 2016 at 2:49
  • Yes, I used df = data.frame(df1$size,df2$size), then melt and finally plotting for dataframes with the equal row number, but I have a problem with combining dataframes with different row numbers. Commented Jan 9, 2016 at 8:13

1 Answer 1

1

It seems from the comments that you're actually trying to merge multiple columns and then plot each column individually. The problem, however, is that each of these columns has a different number of rows. Therefore you need to combine them based on some common variable (i.e. row names).

Using the examples from the link you provided:

df1 = data.frame(size=runif(300,300,1200))

#now adding an unequal column
df2 = data.frame(size=df1[c(1:275),])

Now merge the data frames based on row number. "all=TRUE" keeps all the values, "by=0" merges by row.names.

df.all=merge(df1$size,df2$size,by=0,all=TRUE)

#and to order the row names.
df.all=df.all[order(as.numeric(df.all[,1])),]

#finally if you want to remove the NA values
df.all[is.na(df.all)]=0

Does that get you the data.frame you want?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.