使用列表的内容设置数据集的列名
Setting column names of dataset with contents of list
我读了一份 table,其中包含数据 TestTR
和一份包含相应列名的列表 Headrs
。现在我想用 Headrs
:
的内容设置 TestTR
的列名
TestTR:
V1 V2 V3
2 20 200
1 10 100
3 30 300
Headrs:
Name1 Name2 Name3
它应该是这样的:
Name1 Name2 Name3
2 20 200
1 10 100
3 30 300
这是我的代码:
TestTR <- read.table(file="C:/Users/Sabine/Downloads/UCI_HAR_Dataset/mini_tr.txt", nrows=2)
Headrs <- read.table(file="C:/Users/Sabine/Downloads/UCI_HAR_Dataset/features.txt", nrows=3)
colnames(TestTR) <- Headrs
print (class(Headrs)) # gives me "data.frame"
print (dim(Headrs)) # gives me 3 2
print (Headrs)
V1 V2
1 1 tBodyAcc-mean()-X
2 2 tBodyAcc-mean()-Y
3 3 tBodyAcc-mean()-Z
好像是上面的(其实名字不是name1
、Name2
、Name3
--我这里简化了)
这应该有效:
colnames(TestTR) <- c(names(Headrs))
我读了一份 table,其中包含数据 TestTR
和一份包含相应列名的列表 Headrs
。现在我想用 Headrs
:
TestTR
的列名
TestTR:
V1 V2 V3
2 20 200
1 10 100
3 30 300
Headrs:
Name1 Name2 Name3
它应该是这样的:
Name1 Name2 Name3
2 20 200
1 10 100
3 30 300
这是我的代码:
TestTR <- read.table(file="C:/Users/Sabine/Downloads/UCI_HAR_Dataset/mini_tr.txt", nrows=2)
Headrs <- read.table(file="C:/Users/Sabine/Downloads/UCI_HAR_Dataset/features.txt", nrows=3)
colnames(TestTR) <- Headrs
print (class(Headrs)) # gives me "data.frame"
print (dim(Headrs)) # gives me 3 2
print (Headrs)
V1 V2
1 1 tBodyAcc-mean()-X
2 2 tBodyAcc-mean()-Y
3 3 tBodyAcc-mean()-Z
好像是上面的(其实名字不是name1
、Name2
、Name3
--我这里简化了)
这应该有效:
colnames(TestTR) <- c(names(Headrs))