Merging txt files loses observations

Hi there,

I am very new to R and having trouble combining 4 txt files into one dataframe. This is the code I am using

df2012 <- read.table(file = "/Users/u8008006/Documents/R training/Master_R_for_data_science/Environmental_Data_Deep_Moor_2012.txt", header = TRUE)
df2013 <- read.table(file = "/Users/u8008006/Documents/R training/Master_R_for_data_science/Environmental_Data_Deep_Moor_2013.txt", header = TRUE)
df2014 <- read.table(file = "/Users/u8008006/Documents/R training/Master_R_for_data_science/Environmental_Data_Deep_Moor_2014.txt", header = TRUE)
df2015 <- read.table(file = "/Users/u8008006/Documents/R training/Master_R_for_data_science/Environmental_Data_Deep_Moor_2015.txt", header = TRUE)

data1 = merge(df2012, df2013)
data1 = merge(data1, df2014)
data1 = merge(data1, df2015)

Once I start the merging process I can see in the Global Environment that data1 contains the 9 variables by 0 observations, when it should contain 14,000. What am I missing or doing wrong?

Thank you for your help!

The merge function combines data frames column-wise. It lines up rows that have the same values in certain columns. Since you do not specify any columns to use for the matching, it uses all of the columns that exist in both data frames. If the data frames have all of the same columns, it will keep only those rows that are identical in df2012 and df2013. I doubt that is what you want.
If you want to join the data side-by-side, you can use merge but you need to specify which columns to use in the matching.
If you want to stack the data row-wise, use rbind.

Thank you @FJCC - you were correct in your assumption. Swapping 'merge' with 'rbind' worked exactly as I wanted.

Thank you again!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.