当前位置: 动力学知识库 > 问答 > 编程问答 >

Using a txt file to name multiple colums in r

问题描述:

I want to name columns in a data.frame in r by using a txt file. I manipulated the original txt file to extract the text and made a list out of the prospective column names. But the problem is that I ended up seeing remnants of the list displayed in the column name which is not desired. I would appreciate suggestions what to do instead of creating a list? How to get the text and use it to name columns in r?

The columns names of the dataframe looks like this at the end of my code:

> names(mergedData)[3:6]

[1] "list(V2 = \"tBodyAcc-mean()-X\")" "list(V2 = \"tBodyAcc-mean()-Y\")"

[3] "list(V2 = \"tBodyAcc-mean()-Z\")" "list(V2 = \"tBodyAcc-std()-X\")"

This is what I did:

#read in the txt file

features<-read.table("C:/Adatok/coursera/3_getting and cleaning data/Quizes/Assignment/UCI HAR Dataset/features.txt", stringsAsFactors = FALSE)

> features[1:3,1:2]

V1 V2

1 1 tBodyAcc-mean()-X

2 2 tBodyAcc-mean()-Y

3 3 tBodyAcc-mean()-Z

#removed the first column with this command

features$V1<-NULL

#made a list

features.list<-split(features, seq(nrow(features)))

#named columns

colnames(mergedData)[3:563]<- features.list

分享给朋友:
您可能感兴趣的文章:
随机阅读: