0

Despite reading several topics as listed bellow, I did not find my answer:

R keep data frame attributes after join

Merging two data frames

How to join (merge) data frames (inner, outer, left, right)

I have two txt files containing Year, Month, Day, and two types of weather info (precipitation, and minimum temperature):

enter image description here

enter image description here

You could download these two simple txt files here:

sample_txt_data

I try to join these two data frames using this code:

library(dplyr)
setwd("C:/Users/Climate_Data")

Alberta_Pr <- read.csv("pr_day_Alberta.txt", header=TRUE, sep="\t") 
head(Alberta_Pr)

Alberta_Tasmin <- read.csv("tasmin_day_Alberta.txt", header=TRUE, sep="\t")
head(Alberta_Tasmin)

Joined_data <- full_join(Alberta_Pr, Alberta_Tasmin, by = c("Year", "Month", "Day"))
head(Joined_data)

But using full_join function, remove zeros from month and day columns:

enter image description here

How can I keep the data as it is?

Yours comments or answers would be highly appreciated.

Canada2015
  • 187
  • 1
  • 12
  • 3
    Its easier to help you if you provide some sample data with `dput()` – mnist Oct 31 '19 at 22:39
  • There seems to be confusion about the internal representation of numbers and how they are formatted in R. For example, consider `x <- 1:3`; you can format them with a leading zero using e.g. `sprintf("%02i", x)`. When you read in your data with `read.csv`, R automatically tries to guess the format of entries in the different column. In your case, it seems that columns `Month` and `Day` become `num` (or `int`). To *print* them in a particular format (e.g. using leading zeros), you need to use `sprintf` (or `formatC`). – Maurits Evers Oct 31 '19 at 23:14
  • PS. Take a look at this useful post [How to add leading zeros?](https://stackoverflow.com/questions/5812493/how-to-add-leading-zeros) – Maurits Evers Oct 31 '19 at 23:17
  • 1
    You *could* use `read.csv` and prevent R from guessing column types by explicitly specifying `colClasses = "character"`. In that case all columns would be `character` vectors. I would advise against this though, as it makes *working* with the data more difficult (e.g. it requires you to convert `character` vectors to `num`/`int` vectors manually if you wanted to do some calculations involving `Month` and `Day`). – Maurits Evers Oct 31 '19 at 23:22
  • So in short: The join (seems to have) worked. The issue is with understanding the difference between how data are stored and shown in R. – Maurits Evers Oct 31 '19 at 23:24

0 Answers0