user2603386
user2603386

Reputation:

Set a dataframe as column names for another data frame

So I have a data frame (called SNPlist) which is of dimensions 1 by 500000 (rows by columns). I want SNP list to be the column names for my dataframe of data (called Data) which is of dimension 100 by 500000. I already tried colnames(Data) <- SNPlist but it does not seem to be working. Can anyone help with this issue?

Upvotes: 4

Views: 13244

Answers (3)

Amos
Amos

Reputation: 43

It's been a while since the question was asked, but I noticed nobody suggested what works for me: colnames(Data) <- colnames(SNPlist)

Amos

Upvotes: 3

J.M.S.
J.M.S.

Reputation: 105

I was having a lot of trouble with this because the usual method (provided above by alexwhan) wasn't getting it done. I eventually got around it by first ripping out a headers vector from the old data frame and using the names function to slap it on the new one.

names(new_DF) <- as.character(apply(old_DF["wanted_header_row", ], 1, paste))

Perhaps it's a bit much, but it was the only thing that worked for me.

Upvotes: 0

alexwhan
alexwhan

Reputation: 16026

If SNPlist is a data.frame, then you need to point to the first row of it:

colnames(Data) <- SNPlist[1, ]

If it was a vector, what you'd tried would have worked

Upvotes: 5

Related Questions