将列中的值转换为现有数据框中的行名称 [英] Convert the values in a column into row names in an existing data frame
本文介绍了将列中的值转换为现有数据框中的行名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将现有数据框列中的值转换为行名称.是否可以在不导出数据框然后使用 row.names =
调用重新导入它的情况下执行此操作?
I would like to convert the values in a column of an existing data frame into row names. Is is possible to do this without exporting the data frame and then reimporting it with a row.names =
call?
例如我想转换:
> samp
names Var.1 Var.2 Var.3
1 A 1 5 0
2 B 2 4 1
3 C 3 3 2
4 D 4 2 3
5 E 5 1 4
进入:
> samp.with.rownames
Var.1 Var.2 Var.3
A 1 5 0
B 2 4 1
C 3 3 2
D 4 2 3
E 5 1 4
推荐答案
应该这样做:
samp2 <- samp[,-1]
rownames(samp2) <- samp[,1]
简而言之,除了重新分配之外别无选择.
So in short, no there is no alternative to reassigning.
纠正我自己,也可以就地做:分配rowname属性,然后删除列:
Correcting myself, one can also do it in place: assign rowname attributes, then remove column:
R> df<-data.frame(a=letters[1:10], b=1:10, c=LETTERS[1:10])
R> rownames(df) <- df[,1]
R> df[,1] <- NULL
R> df
b c
a 1 A
b 2 B
c 3 C
d 4 D
e 5 E
f 6 F
g 7 G
h 8 H
i 9 I
j 10 J
R>
这篇关于将列中的值转换为现有数据框中的行名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文