R - Concatenate two dataframes in Regards to Column #? - TagMerge
5R - Concatenate two dataframes in Regards to Column #?R - Concatenate two dataframes in Regards to Column #?

R - Concatenate two dataframes in Regards to Column #?

Asked 5 months ago
0
5 answers

new <- merge(a, b, all.x=TRUE, all.y=TRUE)

Or after

install.packages("dplyr")
require(dplyr)

new <- full_join(a, b)

Source: link

0

The bind_rows() and arrange() functions in the dplyr package should do the trick.

a <- read.table(header=TRUE, text="X         a           b           c
1 -0.2246894 -1.48167912 -1.65099363
5  0.5559320 -0.87898575 -0.15634590
3  1.8469466 -0.01487524 -0.53098215
2 -0.6875051  0.23880967  0.01824621
11 -0.6735163  0.75485292  0.44154092")

b <- read.table(header=TRUE, text="X           a          c
6  0.4287284 -0.3295925
8  0.5201492  0.3341251
7 -2.6355570  1.7916780
9 -1.3645337  1.3642276
10 -0.4954542 -0.6660001")

library(dplyr)
bind_rows(a, b) %>% arrange(X)

Source: link

0

Given two dataframes a and b:
> a
           a           b           c
1 -0.2246894 -1.48167912 -1.65099363
2  0.5559320 -0.87898575 -0.15634590
3  1.8469466 -0.01487524 -0.53098215
4 -0.6875051  0.23880967  0.01824621
5 -0.6735163  0.75485292  0.44154092


> b
           a          c
1  0.4287284 -0.3295925
2  0.5201492  0.3341251
3 -2.6355570  1.7916780
4 -1.3645337  1.3642276
5 -0.4954542 -0.6660001
Is there a simple way to concatenate these so as to return a new data frame of the form below?
> new
           a                   b           c
1  -0.2246894   -1.48167912106676 -1.65099363
2   0.5559320  -0.878985746842256 -0.15634590
3   1.8469466 -0.0148752354840942 -0.53098215
4  -0.6875051   0.238809666690982  0.01824621
5  -0.6735163   0.754852923524198  0.44154092
6   0.4287284                  NA -0.32959248
7   0.5201492                  NA  0.33412510
8  -2.6355570                  NA  1.79167801
9  -1.3645337                  NA  1.36422764
10 -0.4954542                  NA -0.66600006
You want "rbind".
b$b <- NA
new <- rbind(a, b)
Results
> a <- data.frame(a=c(0,1,2), b=c(3,4,5), c=c(6,7,8))
> a
  a b c
1 0 3 6
2 1 4 7
3 2 5 8
> b <- data.frame(a=c(9,10,11), c=c(12,13,14))
> b
   a  c
1  9 12
2 10 13
3 11 14
> b$b <- NA
> b
   a  c  b
1  9 12 NA
2 10 13 NA
3 11 14 NA
> new <- rbind(a,b)
> new
   a  b  c
1  0  3  6
2  1  4  7
3  2  5  8
4  9 NA 12
5 10 NA 13
6 11 NA 14
Try the plyr package:
rbind.fill(a,b,c)

Source: link

0

Name = c("Ram", "Fredo", "Geeta", "Jessica") rank = c(3,4,1,2) marks = c(50, 45, 95, 80) dataframe1 = data.frame(Name,rank,marks) print(dataframe1)
Name rank marks
1     Ram    3    50
2   Fredo    4    45
3   Geeta    1    95
4 Jessica    2    80
Name = c("Suresh", "Ramesh") rank = c(6,5) marks = c(40,43) dataframe2 = data.frame(Name,rank,marks) print(dataframe2)
Name rank marks
1 Suresh    6    40
2 Ramesh    5    43
# combining the rows of the two dataframes dataframe3 = rbind(dataframe1,dataframe2) print(dataframe3)
Name rank marks
1     Ram    3    50
2   Fredo    4    45
3   Geeta    1    95
4 Jessica    2    80
5  Suresh    6    40
6  Ramesh    5    43
# combining the columns of the two dataframes dataframe4 = cbind(dataframe1,dataframe2) print(dataframe4)
Name rank marks   Name rank marks
1     Ram    3    50 Suresh    6    40
2   Fredo    4    45 Ramesh    5    43
3   Geeta    1    95 Suresh    6    40
4 Jessica    2    80 Ramesh    5    43

Source: link

0

data1 <- data.frame(id = 1:6,                                  # Create first example data frame
                    x1 = c(5, 1, 4, 9, 1, 2),
                    x2 = c("A", "Y", "G", "F", "G", "Y"))
 
data2 <- data.frame(id = 4:9,                                  # Create second example data frame
                    y1 = c(3, 3, 4, 1, 2, 9),
                    y2 = c("a", "x", "a", "x", "a", "x"))
merge(data1, data2, by = "id")                                 # Merge data frames by columns names
merge(data1, data2, by = "id", all.x = TRUE)                   # Keep all rows of x-data
merge(data1, data2, by = "id", all.y = TRUE)                   # Keep all rows of y-data
merge(data1, data2, by = "id", all.x = TRUE, all.y = TRUE)     # Keep all rows of both data frames

Source: link

Recent Questions on r

    Programming Languages