Add columns to a table of varied rows
1
1
Entering edit mode
4.9 years ago
radnay ▴ 10

Hi, I am trying to find a way to add the columns to the existing table, where the number of rows in the "adding column" is different from "existing table." I was trying dplyr or should I look for something altogether different.

Thank you.

Good Day

R • 971 views
ADD COMMENT
0
Entering edit mode

Please provide example input and expected output.

ADD REPLY
0
Entering edit mode

The example input

Gender_1  Age_1
1            23
1             35     
2             47  
2             58

The table where it should be inserted

LungCap Age Height  Smoke   Gender  Caesarean
6.475   6   62.1    no  male    no
10.125  18  74.7    yes female  no
9.55    16  69.7    no  female  yes
11.125  14  71  no  male    no
4.8 5   56.9    no  male    no
6.225   11  58.7    no  female  no
4.95    8   63.3    no  male    yes
7.325   11  70.4    no  male    no
8.875   15  70.5    no  male    no

I want to add the input column to the side of existing table and number of rows differs between and I don`t want the data to be recycled

ADD REPLY
0
Entering edit mode

Looks like a job for merge, what is the expected output for this example data?

ADD REPLY
0
Entering edit mode
LungCap Age Height  Smoke   Gender  Caesarean Gender_1  Age_1
6.475   6   62.1    no  male    no 1 23
10.125  18  74.7    yes female  no 1 35
9.55    16  69.7    no  female  yes 2 47
11.125  14  71  no  male    no 2 58
4.8 5   56.9    no  male    no
6.225   11  58.7    no  female  no
4.95    8   63.3    no  male    yes
7.325   11  70.4    no  male    no
8.875   15  70.5    no  male    no

This is what I want.

ADD REPLY
1
Entering edit mode
4.9 years ago
zx8754 11k

Add id column then use merge:

df1$id <- seq(nrow(df1)) 
df2$id <- seq(nrow(df2)) 

merge(df1, df2, by = "id", all.x = TRUE)
#   id LungCap Age Height Smoke Gender Caesarean Gender_1 Age_1
# 1  1   6.475   6   62.1    no   male        no        1    23
# 2  2  10.125  18   74.7   yes female        no        1    35
# 3  3   9.550  16   69.7    no female       yes        2    47
# 4  4  11.125  14   71.0    no   male        no        2    58
# 5  5   4.800   5   56.9    no   male        no       NA    NA
# 6  6   6.225  11   58.7    no female        no       NA    NA
# 7  7   4.950   8   63.3    no   male       yes       NA    NA
# 8  8   7.325  11   70.4    no   male        no       NA    NA
# 9  9   8.875  15   70.5    no   male        no       NA    NA

Or Using custom function from StackOverflow post - Column bind with fill:

cbind.fill <- function(...){
  nm <- list(...) 
  nm <- lapply(nm, as.matrix)
  n <- max(sapply(nm, nrow)) 
  do.call(cbind, lapply(nm, function (x) 
    rbind(x, matrix(, n-nrow(x), ncol(x))))) 
}

result <- data.frame(cbind.fill(df1, df2))
result
#   LungCap Age Height Smoke Gender Caesarean Gender_1 Age_1
# 1   6.475   6   62.1    no   male        no        1    23
# 2  10.125  18   74.7   yes female        no        1    35
# 3   9.550  16   69.7    no female       yes        2    47
# 4  11.125  14   71.0    no   male        no        2    58
# 5   4.800   5   56.9    no   male        no     <NA>  <NA>
# 6   6.225  11   58.7    no female        no     <NA>  <NA>
# 7   4.950   8   63.3    no   male       yes     <NA>  <NA>
# 8   7.325  11   70.4    no   male        no     <NA>  <NA>
# 9   8.875  15   70.5    no   male        no     <NA>  <NA>

Example data input:

df1 <- read.table(text = "LungCap Age Height  Smoke   Gender  Caesarean
6.475   6   62.1    no  male    no
10.125  18  74.7    yes female  no
9.55    16  69.7    no  female  yes
11.125  14  71  no  male    no
4.8 5   56.9    no  male    no
6.225   11  58.7    no  female  no
4.95    8   63.3    no  male    yes
7.325   11  70.4    no  male    no
8.875   15  70.5    no  male    no", header = TRUE)

df2 <- read.table(text = "Gender_1  Age_1
1            23
1             35     
2             47  
2             58", header = TRUE)
ADD COMMENT
0
Entering edit mode

Thanks a lot. My problem was solved.

Good Day

ADD REPLY
0
Entering edit mode

If an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one if they work.
Upvote|Bookmark|Accept

ADD REPLY

Login before adding your answer.

Traffic: 2426 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6