问题描述
我有多个要导入到 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() )