1

I am using R and original datasets are available in the RStudio environment. After merging datasets, I lost labels of the variables. Is there any way to get them back? I saw this link but I couldn't solve my problem yet.

df<-read.table(text="No.  addr  x num
1    a  1   1
2    b NA   2
3    c  4   3
4    d  5   4
5    e  5  10
6    e  2 100
7    f  7  7
8    f  1 200
9    g  NA 500 ", sep="", header=T)

var.labels <- c(No. = "Serial number", addr = "Addeded response", x="Events", num="Number of particles")

attr(df, "variable.labels") <- var.labels

df1<-read.table(text="No.  addr  x num
1    a  1   1
2    b NA   2
10   h  7  7
11   i  1 200", sep="", header=T)


var<-intersect(names(df), names(df1));var
df1<-merge(x = df, y = df1, by = var, all=T);df3


df1<-read.table(text="No.  addr  x num
1    a  1   1
2    b NA   2
3    c  4   3
4    d  5   4
5    e  5  10
6    e  2  100
7    f  7  7
8    f  1  200
9    g  NA 500 
10   h  7  7
11   i  1 200", sep="", header=T)


I don't have this line attr(df, "variable.labels") <- var.labels but I put it here just for representable example.

Any advice will be greatly appreciated.

Mohamed Rahouma
  • 1,084
  • 9
  • 20

1 Answers1

2

Instead of merge, we could use full_join from dplyr which would keep the attributes as such

library(dplyr)
df3 <- full_join(df, df1, by = var)

str(df3)
#'data.frame':  11 obs. of  4 variables:
# $ No. : int  1 2 3 4 5 6 7 8 9 10 ...
# $ addr: chr  "a" "b" "c" "d" ...
# $ x   : int  1 NA 4 5 5 2 7 1 NA 7 ...
# $ num : int  1 2 3 4 10 100 7 200 500 7 ...
# - attr(*, "variable.labels")= Named chr [1:4] "Serial number" "Addeded response" #"Events" "Number of particles"
#  ..- attr(*, "names")= chr [1:4] "No." "addr" "x" "num"
akrun
  • 874,273
  • 37
  • 540
  • 662