【问题标题】:How to mutate columns but keep rownames in R pipe?如何改变列但在 R 管道中保留行名?
【发布时间】:2020-06-16 12:19:18
【问题描述】:
r$> iris %>% .[which(as.numeric(rownames(.))%%3!=0),] %>% rownames
  [1] "1"   "2"   "4"   "5"   "7"   "8"   "10"  "11"  "13"  "14"  "16"  "17"  "19"  "20"  "22"  "23"  "25"  "26"  "28"  "29"  "31"  "32"  "34"  "35"  "37"  "38"  "40"  "41"  "43"  "44"  "46"
 [32] "47"  "49"  "50"  "52"  "53"  "55"  "56"  "58"  "59"  "61"  "62"  "64"  "65"  "67"  "68"  "70"  "71"  "73"  "74"  "76"  "77"  "79"  "80"  "82"  "83"  "85"  "86"  "88"  "89"  "91"  "92"
 [63] "94"  "95"  "97"  "98"  "100" "101" "103" "104" "106" "107" "109" "110" "112" "113" "115" "116" "118" "119" "121" "122" "124" "125" "127" "128" "130" "131" "133" "134" "136" "137" "139"
 [94] "140" "142" "143" "145" "146" "148" "149"

r$> iris %>% .[which(as.numeric(rownames(.))%%3!=0),] %>% mutate(Sepal.Length=Sepal.Length+1) %>% rownames
  [1] "1"   "2"   "3"   "4"   "5"   "6"   "7"   "8"   "9"   "10"  "11"  "12"  "13"  "14"  "15"  "16"  "17"  "18"  "19"  "20"  "21"  "22"  "23"  "24"  "25"  "26"  "27"  "28"  "29"  "30"  "31"
 [32] "32"  "33"  "34"  "35"  "36"  "37"  "38"  "39"  "40"  "41"  "42"  "43"  "44"  "45"  "46"  "47"  "48"  "49"  "50"  "51"  "52"  "53"  "54"  "55"  "56"  "57"  "58"  "59"  "60"  "61"  "62"
 [63] "63"  "64"  "65"  "66"  "67"  "68"  "69"  "70"  "71"  "72"  "73"  "74"  "75"  "76"  "77"  "78"  "79"  "80"  "81"  "82"  "83"  "84"  "85"  "86"  "87"  "88"  "89"  "90"  "91"  "92"  "93"
 [94] "94"  "95"  "96"  "97"  "98"  "99"  "100"

我喜欢mutate(),因为它很容易在管道中使用。如上例所示,您可以发现 mutate 后行名发生了变化。

我需要改变列但保持行名不变,如何通过 R 管道进行?

【问题讨论】:

  • 这与管道无关。 mutate 删除行名。
  • @KonradRudolph,我需要管道格式的解决方案。
  • @kittygirl 是的,但如前所述,这对于问题或潜在答案都不重要,因为管道操作员的存在与否对此没有影响。除此之外,如果您想保留行名,则不能使用mutate 等。但我强烈建议您坚持使用mutate,并习惯于没有行名。我也花了一些时间,但当我习惯将行名视为另一列后,我意识到不使用行名绝对没有缺点,而且有很多优点。

标签: r


【解决方案1】:

这是因为mutate 或一般dplyr 在任何操作后从 1 重新调整行名,因此它不会保留原始行名。

如果您需要它们进行进一步操作,请将它们存储为列。

library(dplyr)

iris %>% 
   .[which(as.numeric(rownames(.))%%3!=0),] %>%
    mutate(row = rownames(.),
            Sepal.Length=Sepal.Length+1) %>%
    pull(row)

#  [1] "1"   "2"   "4"   "5"   "7"   "8"   "10"  "11"  "13"  "14"  "16"  "17"  "19"  "20"  "22"  "23"  "25"  "26" 
# [19] "28"  "29"  "31"  "32"  "34"  "35"  "37"  "38"  "40"  "41"  "43"  "44"  "46"  "47"  "49"  "50"  "52"  "53" 
# [37] "55"  "56"  "58"  "59"  "61"  "62"  "64"  "65"  "67"  "68"  "70"  "71"  "73"  "74"  "76"  "77"  "79"  "80" 
# [55] "82"  "83"  "85"  "86"  "88"  "89"  "91"  "92"  "94"  "95"  "97"  "98"  "100" "101" "103" "104" "106" "107"
# [73] "109" "110" "112" "113" "115" "116" "118" "119" "121" "122" "124" "125" "127" "128" "130" "131" "133" "134"
# [91] "136" "137" "139" "140" "142" "143" "145" "146" "148" "149"

【讨论】:

  • 那么,如何获取带有origin rowname的iris
  • 小标题不支持行名,因此您需要将它们添加为单独的列。
  • mutate 的任何替代函数?
  • 你想做什么?
  • 我要处理column并保留rowname
【解决方案2】:
iris %>% tibble::rownames_to_column(., 'rowname') %>% .[which(as.numeric(rownames(.))%%3!=0),] %>% mutate(Sepal.Length=Sepal.Length+1) %>% tibble::column_to_rownames(.,'rowname')

【讨论】:

  • 虽然此代码可以解决 OP 的问题,但最好包含关于您的代码如何解决 OP 问题的说明。这样,未来的访问者可以从您的帖子中学习,并将其应用到他们自己的代码中。 SO 不是编码服务,而是知识资源。此外,高质量、完整的答案更有可能得到支持。这些功能,以及所有帖子都是独立的要求,是 SO 作为一个平台的一些优势,使其与论坛区分开来。您可以编辑以添加其他信息和/或使用源文档补充您的解释。
【解决方案3】:

拥有行名违反了dplyr 的数据帧结构原则。见哈德利的推理here

如果您想坚持使用 dplyr,最好的解决方法是首先使用 rownames_to_column 将行名存储为列,进行操作,然后使用 column_to_rownames 恢复行名(这两个函数都来自 tibble 包)。

用你的例子:

library(dplyr)
library(tibble)

iris %>% 
.[which(as.numeric(rownames(.))%%3!=0),] %>% 
# store rownames in a column
rownames_to_column(var = "rowid") %>%
# dplyr manipulation
mutate(Sepal.Length=Sepal.Length+1) %>% 
# reinstate rownames
column_to_rownames(var = "rowid") %>% 
# check rownames
rownames()

# [1] "1"   "2"   "4"   "5"   "7"   "8"   "10"  "11"  "13"  "14"  "16"  "17"  "19"  "20"  "22"  "23"  "25"  "26"  "28"  "29"  "31"  "32"  "34"  "35"  "37"  "38" 
# [27] "40"  "41"  "43"  "44"  "46"  "47"  "49"  "50"  "52"  "53"  "55"  "56"  "58"  "59"  "61"  "62"  "64"  "65"  "67"  "68"  "70"  "71"  "73"  "74"  "76"  "77" 
# [53] "79"  "80"  "82"  "83"  "85"  "86"  "88"  "89"  "91"  "92"  "94"  "95"  "97"  "98"  "100" "101" "103" "104" "106" "107" "109" "110" "112" "113" "115" "116"
# [79] "118" "119" "121" "122" "124" "125" "127" "128" "130" "131" "133" "134" "136" "137" "139" "140" "142" "143" "145" "146" "148" "149"

【讨论】:

    猜你喜欢
    • 2010-12-22
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 2016-11-12
    • 1970-01-01
    • 2021-12-19
    • 2022-11-29
    相关资源
    最近更新 更多