如何将第一行的值更改为其在 R 中的行号?

问题描述

我有多个要导入到 R 中的 Excel 工作表。我使用了以下代码

files <- list.files(path = "D:/xxx/Daily Report/",pattern = "*.xlsx",full.names = T)

tbl <- sapply(files,read_xlsx,simplify=FALSE) %>% bind_rows(.id = "S No")

在 S No 列中,填充的值为文件路径。我想将它们转换为行号。当我尝试更改 S No

的值时获得以下输出
tbl <- tbl %>% mutate(.$`S No` = row_number())
Error: unexpected '=' in "tbl <- tbl %>% mutate(.$`S No` ="

解决方法

这应该有效:


tbl <- sapply(files,read_xlsx,simplify=FALSE) %>% bind_rows %>% mutate(.id = 1:n() )

您还可以将 .id 更改为您想要的任何其他列名称。

并为此使用了预期的功能,感谢下面的 OP 和评论者,@Gregor Thomas:


tbl <- sapply(files,simplify=FALSE) %>% bind_rows %>% mutate(.id = row_number() )