R 中有没有一种方法可以对不是整数的特定列进行总计?
Is there a way in R to total specific columns that aren't integers?
确实遇到了 R 中的一个问题,希望能得到一些帮助。
考虑以下 table:
Vars
Democrats(M.F)
Republicans(M.F)
votes(MI)
30 . 53
40 . 23
votes(TX)
64 . 42
45 . 25
votes(COL)
30 . 59
20 . 23
votes(NY)
64 . 40
18 . 34
我想要一个额外的列,以给定的相同 M.F 格式对上述所有值求和,因此:
Vars
Democrats(M.F)
Republicans(M.F)
votes(MI)
30 . 53
40 . 23
votes(TX)
64 . 42
45 . 25
votes(COL)
30 . 59
20 . 23
votes(NY)
64 . 40
18 . 34
TOTAL
188 . 194
123 . 105
有谁知道一个简单的方法来做到这一点?我提出了需要彻底检修 table 的解决方案,而理想情况下我不想要。提前致谢!
示例数据
df <- structure(list(HOW = structure(c(2L, 4L, 3L, 1L), .Label = c("4", "1", "3", "2"), class = "factor"), Democrats = structure(c("1" = 2L, "2" = 4L, "3" = 3L, "4" = 1L), .Label = c("0 . 0", "1 . 2", "3 . 1", "4 . 6"), class = "factor"), Repubs = structure(c("1" = 2L, "2" = 3L, "3" = 4L, "4" = 1L), .Label = c("0 . 2", "1 . 1", "5 . 2", "5 . 7"), class = "factor")), class = "data.frame", row.names = c(NA, -4L))
一个base
解决方案:
df[] <- lapply(df, as.character)
rbind(
df,
c(HOW = "Total", lapply(df[-1], \(x) paste(rowSums(sapply(strsplit(x, " . "), as.numeric)), collapse = " . ")))
)
# HOW Democrats Repubs
# 1 1 1 . 2 1 . 1
# 2 2 4 . 6 5 . 2
# 3 3 3 . 1 5 . 7
# 4 4 0 . 0 0 . 2
# 5 Total 8 . 9 11 . 12
这是一个 dplyr
答案。
library(tidyverse)
df <- structure(list(HOW = structure(c(2L, 4L, 3L, 1L), .Label = c("4", "1", "3", "2"), class = "factor"), Democrats = structure(c("1" = 2L, "2" = 4L, "3" = 3L, "4" = 1L), .Label = c("0 . 0", "1 . 2", "3 . 1", "4 . 6"), class = "factor"), Repubs = structure(c("1" = 2L, "2" = 3L, "3" = 4L, "4" = 1L), .Label = c("0 . 2", "1 . 1", "5 . 2", "5 . 7"), class = "factor")), class = "data.frame", row.names = c(NA, -4L))
df2 <- df %>%
separate(Democrats, c("Democrats.M", "Democrats.F")) %>%
separate(Repubs, c("Repubs.M", "Repubs.F")) %>%
mutate(across(-HOW, as.integer))
df2 %>%
summarize(HOW = "Total", across(-HOW, sum)) %>%
bind_rows(df2, .) %>%
unite("Democrats", starts_with("Democrats."), sep = " . ") %>%
unite("Repubs", starts_with("Repubs."), sep = " . ")
但老实说,我建议将数据放在单独的列中,并且只在最后合并显示。
确实遇到了 R 中的一个问题,希望能得到一些帮助。
考虑以下 table:
Vars | Democrats(M.F) | Republicans(M.F) |
---|---|---|
votes(MI) | 30 . 53 | 40 . 23 |
votes(TX) | 64 . 42 | 45 . 25 |
votes(COL) | 30 . 59 | 20 . 23 |
votes(NY) | 64 . 40 | 18 . 34 |
我想要一个额外的列,以给定的相同 M.F 格式对上述所有值求和,因此:
Vars | Democrats(M.F) | Republicans(M.F) |
---|---|---|
votes(MI) | 30 . 53 | 40 . 23 |
votes(TX) | 64 . 42 | 45 . 25 |
votes(COL) | 30 . 59 | 20 . 23 |
votes(NY) | 64 . 40 | 18 . 34 |
TOTAL | 188 . 194 | 123 . 105 |
有谁知道一个简单的方法来做到这一点?我提出了需要彻底检修 table 的解决方案,而理想情况下我不想要。提前致谢!
示例数据
df <- structure(list(HOW = structure(c(2L, 4L, 3L, 1L), .Label = c("4", "1", "3", "2"), class = "factor"), Democrats = structure(c("1" = 2L, "2" = 4L, "3" = 3L, "4" = 1L), .Label = c("0 . 0", "1 . 2", "3 . 1", "4 . 6"), class = "factor"), Repubs = structure(c("1" = 2L, "2" = 3L, "3" = 4L, "4" = 1L), .Label = c("0 . 2", "1 . 1", "5 . 2", "5 . 7"), class = "factor")), class = "data.frame", row.names = c(NA, -4L))
一个base
解决方案:
df[] <- lapply(df, as.character)
rbind(
df,
c(HOW = "Total", lapply(df[-1], \(x) paste(rowSums(sapply(strsplit(x, " . "), as.numeric)), collapse = " . ")))
)
# HOW Democrats Repubs
# 1 1 1 . 2 1 . 1
# 2 2 4 . 6 5 . 2
# 3 3 3 . 1 5 . 7
# 4 4 0 . 0 0 . 2
# 5 Total 8 . 9 11 . 12
这是一个 dplyr
答案。
library(tidyverse)
df <- structure(list(HOW = structure(c(2L, 4L, 3L, 1L), .Label = c("4", "1", "3", "2"), class = "factor"), Democrats = structure(c("1" = 2L, "2" = 4L, "3" = 3L, "4" = 1L), .Label = c("0 . 0", "1 . 2", "3 . 1", "4 . 6"), class = "factor"), Repubs = structure(c("1" = 2L, "2" = 3L, "3" = 4L, "4" = 1L), .Label = c("0 . 2", "1 . 1", "5 . 2", "5 . 7"), class = "factor")), class = "data.frame", row.names = c(NA, -4L))
df2 <- df %>%
separate(Democrats, c("Democrats.M", "Democrats.F")) %>%
separate(Repubs, c("Repubs.M", "Repubs.F")) %>%
mutate(across(-HOW, as.integer))
df2 %>%
summarize(HOW = "Total", across(-HOW, sum)) %>%
bind_rows(df2, .) %>%
unite("Democrats", starts_with("Democrats."), sep = " . ") %>%
unite("Repubs", starts_with("Repubs."), sep = " . ")
但老实说,我建议将数据放在单独的列中,并且只在最后合并显示。