How to combine different dimension (both frame has one column with different sort and dimension)

r

#1

how to combine different dimension (both frame has one column with different sort and dimension) in R
thanks for reading my question
thanks a lot if any1 helps


#2

Hello Narayana,

Could you share an example of both data frames?

If you want to combine two data frames with one column of different data types and different length in one data frame, you can use cbind but make sure the length of column in both df is same by padding NA to end of shorter data frame.

a simulation is as below:

x <- data.frame(x=c(1,2,3,4,5))
y <- data.frame(y=c(β€˜a’,β€˜b’))

num_na <- dim(x)[1]-dim(y)[1]

cbind.data.frame(X=x$x,Y=c(as.character(y$y),rep(NA,num_na)))

Note: used as.character to convert the factor to character

I hope this helps


#3

there are two data sets .

one with user_id and date (data order by date) and repeat user_ids
in 2nd data, no repeated User_id with frequency column.
user_ids are same in both data but dimensions are different.
my question how to combine these two data sets by user_id

eg: data1: user-id,date
0001 27/1
0002 12/3
0003 13/4
0001 27/4
0002 06/7
date2: user-id,freq
0001 2
0002 2
0003 1
I want output as
data 3:user-id,date.freq
0001 27/1 2
0002 12/3 2
0003 13/4 1
0001 27/4 2
0002 06/7 2


#4

Hello Narayan,

you can use merge to combine the two data set. Use the following code.

data1 <- data.frame(user_id = c(β€˜0001’,β€˜0002’,β€˜0003’,β€˜0001’,β€˜0002’),
date = c(β€˜27/1’,β€˜12/3’,β€˜13/4’,β€˜27/4’,β€˜06/7’))

data2 <- data.frame(user_id = c(β€˜0001’,β€˜0002’,β€˜0003’),
date.freq = c(2,2,1))

# merge by user id

data3 <- merge(data1,data2, by = β€˜user_id’)
data3

Below is the screen capture of simulation. The merged data frame is sorted by the column on which it is merged.


#5

#eg: data1: user-id,date
dat1 = data.frame(user_id = c(1,2,3,1,2), date =c( β€œ27/1”,β€œ12/3”,β€œ13/4”,
β€œ27/4”,β€œ06/7”))
#date2: user-id,freq
dat2 = data.frame(user_id = c(1,2,3), freq = c(2,2,1))

This is what you are looking for !

merge(dat1, dat2, by.x = β€œuser_id”, by.y = β€œuser_id”, all.x = TRUE)


#6

Thank you sir


#7

Thanks a lot


#8

You are welcome !! Happy Learning :slight_smile:


#9

I have a problem to combine two different dimension dataframes which each dataframe has huge rows. Let’s say, the sample of my dataframes are d and e, and new expected dataframe is de. I would like to make pair between all value in same row both in d and e, and construct those pairs in a new dataframe (de). Any idea/help for solving my problem is really appreciat
data science