簡體   English   中英

在 R 中將列名重命名為 null

[英]rename column names as null in R

有沒有辦法在 R 中將列名重命名為 NULL 舉例

head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
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

預期 output (所以第一列名稱應該為空)

head(iris)
               Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
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

我們不能重命名為NULL ,但可以是空白( ""

names(iris)[1] <- ""

-輸出

 head(iris)
      Sepal.Width Petal.Length Petal.Width Species
1 5.1         3.5          1.4         0.2  setosa
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

如果我們想要tidyverse

library(dplyr)
iris %>% 
    rename_with(~ sQuote("", FALSE), .cols = 1) %>% 
    head
   '' Sepal.Width Petal.Length Petal.Width Species
1 5.1         3.5          1.4         0.2  setosa
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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM