Zach
Zach

Reputation: 1396

Set colnames in data frame based upon vector of names in R

I have a data frame output with dim() characteristics of 563 (y) variables and 2947 observations (x). Columns 1 - 2 are subject #s and test labels whereas 3-563 are the names of the variable observations.

I have a separate data frame df-vars containing 561 variable names. How can I set the column names of my original data frame output to the 561 variable names if df-vars?

df-vars looks like this:

    V1  V2
1   1   tBodyAcc-mean()-X
2   2   tBodyAcc-mean()-Y
3   3   tBodyAcc-mean()-Z
4   4   tBodyAcc-std()-X
5   5   tBodyAcc-std()-Y
6   6   tBodyAcc-std()-Z
7   7   tBodyAcc-mad()-X

Upvotes: 1

Views: 3719

Answers (1)

copeg
copeg

Reputation: 8348

How can I set the column names of my original df (output) to the 561 variable names (df-vars)

You can set a subset of the column names of output to the appropriate names within df-vars, for instance if df-vars contains the column names in column 2:

colnames(output)[3:563] = as.character(df-vars[,2]);

Upvotes: 1

Related Questions