Eric González
Eric González

Reputation: 485

rbind for named vectors Named num

I obtained two named vectors from ColSums doing something like this:

MA <- ColSums(DF==1)
MAF<- MA/nrow(DF)

>str(MAF)
Named num [1:501124] 0.435 0.4502 0.5498 0 0.0785 ...
- attr(*, "names")= chr [1:501124] "AX-90649662" "AX-90649663" "AX-91377601" "AX-90649678" ...

Now want to merge two vectors that look like this:

head(MAF)

AX-90649662 AX-90649663 AX-91377601 AX-90649678 AX-91453449 AX-90649677
0.43504532  0.45015106  0.54984894  0.00000000  0.07854985  0.00000000

head(MAY_counts)

AX-90649662 AX-90649663 AX-91377601 AX-90649678 AX-91453449 AX-90649677
0.5649547   0.5498489   0.4501511   1.0000000   0.9214502   1.0000000

if I use a simple rbind like this:

rbind(MAF, MAY_counts)

My output now have 5 rows and 3 of them are filled witg NAs

f I use something like this:

 rbind(as.data.frame(MAF),  as.data.frame(MAY_counts))

I have the error:

 Error in match.names(clabs, names(xi)) :
 names do not match previous names

However the names of my dataframes are identical:

 > identical(names(MAY_counts), names(MAF) )
 [1] TRUE

My expected output is the next data frame:

  AX-90649662 AX-90649663 AX-91377601 AX-90649678 AX-91453449 AX-90649677
1 0.43504532  0.45015106  0.54984894  0.00000000  0.07854985  0.00000000
2 0.5649547   0.5498489   0.4501511   1.0000000   0.9214502   1.0000000

Could someone tell me how I could achieve this?

I added some extra information to answer the questions.

Upvotes: 0

Views: 216

Answers (2)

TobKel
TobKel

Reputation: 1453

Try this:

dplyr::bind_rows(MAF, MAY_counts)

Upvotes: 0

ThomasIsCoding
ThomasIsCoding

Reputation: 102251

I think using rbind() is enough. Assuming A and B are named vectors as below

A <- `names<-`(1:5,letters[1:5])
B <- `names<-`(6:10,letters[1:5])

then rbind() makes:

AB <- rbind(A,B)
> AB
  a b c d  e
A 1 2 3 4  5
B 6 7 8 9 10

If you further want AB as a data frame, then use data.frame()

AB <- data.frame(AB,row.names = NULL)

such that

> AB
  a b c d  e
1 1 2 3 4  5
2 6 7 8 9 10

Upvotes: 1

Related Questions