0

当我将我的数据框从 R 导出到 excel 时,最后一列最终在 excel 中没有值。请问有什么建议吗?

我通过这段代码导出:

write_xlsx(X,"/Users/MT/Desktop/Analysis v2/Frequency_aez.xlsx")

我的数据结构:

dput(aez_frequency[1:2, c(1,11, 12, 13, 14) ])
structure(list(GID = c(143696, 143697), Diff1 = c(0, 0), Diff2 = c(0, 0), Diff3 = c(0, 0), Frequency_AEZ_change = structure(list(GID = c(143696, 143697), Country = c("Abyei", "Abyei"), File_name_1 =c("PRIO_AEZ_FS_1981_2010",  "PRIO_AEZ_FS_1981_2010"), AEZ_1 = c("Tropics, lowland semi-arid", "Tropics, lowland semi-arid"), File_name_2 = c("PRIO_AEZ_FS_2011_2040", "PRIO_AEZ_FS_2011_2040"), AEZ_2 = c("Tropics, lowland semi-arid", "Tropics, lowland semi-arid"), File_name_3 = c("PRIO_AEZ_FS_2041_2070", "PRIO_AEZ_FS_2041_2070"), AEZ_3 = c("Tropics, lowland semi-arid", "Tropics, lowland semi-arid"), File_name_4 = c("PRIO_AEZ_FS_2071_2100", "PRIO_AEZ_FS_2071_2100"), AEZ_4 = c("Tropics, lowland semi-arid", "Tropics, lowland semi-arid"), Diff1 = c(0, 0), Diff2 = c(0, 0), Diff3 = c(0, 0), Frequency_AEZ_change = c(0, 0)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -2L))), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"))

最后一列是用 tidyverse 计算的

library(tidyverse)

aez_frequency$Frequency_AEZ_change<-aez_frequency %>% mutate(Frequency_AEZ_change= select(., Diff1:Diff3) %>%
       rowSums(na.rm = TRUE))

谢谢 !

4

1 回答 1

0

那是因为代码返回一个嵌套的数据框。

尝试rowSums以这种方式添加新列。

library(dplyr)

aez_frequency <- aez_frequency %>% mutate(Frequency_AEZ_change= rowSums(select(., Diff1:Diff3))) 

writexl::write_xlsx(aez_frequency,"/Users/MT/Desktop/Analysis v2/Frequency_aez.xlsx")
于 2021-09-22T11:28:33.137 回答