将数据框行转换为列名 [英] Convert data frame row to column names
本文介绍了将数据框行转换为列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种快速的方法(可能是 tidyverse
API的一部分)将行变成 data.frame $的列名c $ c>或
tibble
,有点类似于 tibble :: column_to_rownames
?
Is there a quick way (part of the tidyverse
API perhaps) to turn a row into column names for a data.frame
or tibble
, somewhat similar to tibble::column_to_rownames
?
我知道有很多方法可以做到这一点,例如有点笨拙:
I realize there are many ways to do this, e.g. somewhat clumsily:
> df <- head(iris)
>
> df %>%
+ set_colnames(magrittr::extract(., 1,)) %>%
+ magrittr::extract(-1,)
5.1 3.5 1.4 0.2 1
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
推荐答案
janitor :: row_to_names()
实现了这一点:
janitor::row_to_names()
implements this:
library(tidyverse)
iris %>%
head() %>%
janitor::row_to_names(1)
#> 5.1 3.5 1.4 0.2 1
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
由 reprex包(v0.2.1)
Created on 2019-05-29 by the reprex package (v0.2.1)
这篇关于将数据框行转换为列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文