-2

I have 2 data frames, i want apply aggregate from all data frame, my data is very large, so.. suppose my data is like:

data1
ID   Name    
1     A        
2     B       
3     C       
4     D       
5     E      
6     F       



data2
ID   Age
1      4 
2      5
3      12
4      53
5      2
6      8

after apply aggregate function, i want these result:

data_result
ID   Name    Age
1     A       4 
2     B       5
3     C       12
4     D       53
5     E       2
6     F       8

small notes:

  1. data frame not have same length ( rows and cols ).
  2. i was try to use group by function, but not work, show me this error:

    Error in UseMethod("group_by_") : 
    no applicable method for 'group_by_' applied to an object of class 
    "character"
    
  3. I don't need all columns from data frame, just 2 column for each one of them.

  4. these task is very easy to implement in excel, but in r ???

any one can help me ?

Thanks

A. Suliman
  • 12,923
  • 5
  • 24
  • 37
Mj___3
  • 13
  • 8
  • u can merge the data frames first and make the dataframe with equal length otherwise while aggregating u will get misleading values. – sai saran Oct 29 '18 at 06:02
  • doesn't work, it seems: 'by' must specify a uniquely valid column – Mj___3 Oct 29 '18 at 06:05

2 Answers2

0

You can merge the 2 dataframes together on ID with

merge(data1, data2, by="ID")
Zito Relova
  • 1,041
  • 2
  • 13
  • 32
0

use merge() function in R as follows:

merge (x = dataframe1, y= dataframe2, by = "commom_column")

if data frames have different lengths its not a problem. For selecting columns, you can use select() function in dplyr package as follows:

dataframe1 <- select(dataframe1, c(column_names))

For your problem:

data1 <- select(data1, c(ID, name))
data2 <- select(data1, c(ID, Age))
merge(data1, data2, by = "ID")