【问题标题】:Put the first row as the column names of my dataframe with dplyr in R将第一行作为我的数据框的列名,在 R 中使用 dplyr
【发布时间】:2018-10-07 00:12:49
【问题描述】:

这是我的数据框:

x<-data.frame(A = c(letters[1:10]), M1 = c(11:20), M2 = c(31:40), M3 = c(41:50))

colnames(x)<-NULL

我想转置 (t(x)) 并将 x 的第一列视为新数据框 t(x) 的列名。

我还需要将它们(t(x) 的列名)标识为单词/字母(作为字符对吗?)

dplyr 包可以做到这一点吗?

有什么帮助吗?

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    {janitor} 包对此很有用,并且足够灵活,可以选择任何行来推送到列名:

    
    library(tidyverse)
    library(janitor)
    
    x <- x %>% row_to_names(row_number = 1)
    

    【讨论】:

      【解决方案2】:

      试试这个:

      library(dplyr)
      library(tidyr)
      
      x <- data.frame(
        A = c(letters[1:10]),
        M1 = c(11:20),
        M2 = c(31:40),
        M3 = c(41:50))
      
      x %>% 
        gather(key = key, value = value, 2:ncol(x)) %>% 
        spread(key = names(x)[1], value = "value")
        key  a  b  c  d  e  f  g  h  i  j
      1  M1 11 12 13 14 15 16 17 18 19 20
      2  M2 31 32 33 34 35 36 37 38 39 40
      3  M3 41 42 43 44 45 46 47 48 49 50
      

      【讨论】:

        【解决方案3】:

        您可以在基础 R 中轻松做到这一点。只需将 x 的第一列设为行名,然后删除第一列并转置即可。

        row.names(x) = x[,1]
        x = t(x[,-1])
        x
            a  b  c  d  e  f  g  h  i  j
        M1 11 12 13 14 15 16 17 18 19 20
        M2 31 32 33 34 35 36 37 38 39 40
        M3 41 42 43 44 45 46 47 48 49 50
        

        【讨论】:

          【解决方案4】:

          使用tibble 包中的rownames_to_column()

          library(magrittr)
          library(tibble)
          
          x %>%
            t() %>%
            as.data.frame(stringsAsFactors = FALSE) %>%
            rownames_to_column() %>%
            `colnames<-`(.[1,]) %>%
            .[-1,] %>%
            `rownames<-`(NULL)
          #>    A  a  b  c  d  e  f  g  h  i  j
          #> 1 M1 11 12 13 14 15 16 17 18 19 20
          #> 2 M2 31 32 33 34 35 36 37 38 39 40
          #> 3 M3 41 42 43 44 45 46 47 48 49 50
          
          x %>% 
            `row.names<-`(.[, 1]) %>% 
            t() %>%
            as.data.frame(stringsAsFactors = FALSE) %>% 
            .[-1,] 
          #>     a  b  c  d  e  f  g  h  i  j
          #> M1 11 12 13 14 15 16 17 18 19 20
          #> M2 31 32 33 34 35 36 37 38 39 40
          #> M3 41 42 43 44 45 46 47 48 49 50
          

          reprex package (v0.2.1.9000) 于 2018 年 10 月 6 日创建

          【讨论】:

          • 我希望 slice 在这里工作以保持它在 tidyverse 中。它适用于第二个链,但不是第一个。 ` df %>% colnames% # 不能改成切片 slice(-1) #删除第一行`
          【解决方案5】:

          我认为tibble 包中的column_to_rownames 将是您最简单的解决方案。在使用 t 转置之前使用它。

          library(magrittr)
          library(tibble)
          
          x %>% 
            column_to_rownames("A") %>% 
            t
          
          #>     a  b  c  d  e  f  g  h  i  j
          #> M1 11 12 13 14 15 16 17 18 19 20
          #> M2 31 32 33 34 35 36 37 38 39 40
          #> M3 41 42 43 44 45 46 47 48 49 50
          

          上面的“M1”、“M2”、“M3”是行名。如果您想将它们保留在里面(作为一列),您可以从同一个包中添加rownames_to_column

          x %>% 
            column_to_rownames("A") %>% 
            t %>% 
            as.data.frame %>% 
            rownames_to_column("key")
          
          #>   key  a  b  c  d  e  f  g  h  i  j
          #> 1  M1 11 12 13 14 15 16 17 18 19 20
          #> 2  M2 31 32 33 34 35 36 37 38 39 40
          #> 3  M3 41 42 43 44 45 46 47 48 49 50
          

          基本上,
          column_to_rownames("A")x 中的“A”列转换为行名,
          t 转置 data.frame(现在是矩阵),
          as.data.frame 将其重新归类为 data.frame(这是下一个函数所必需的),并且
          rownames_to_column("key") 将行名转换为名为“key”的新列。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-06-06
            • 1970-01-01
            • 2018-07-29
            • 1970-01-01
            • 1970-01-01
            • 2014-12-15
            • 2018-11-09
            • 1970-01-01
            相关资源
            最近更新 更多