我必须在一个数据帧中添加一个新行total
,在其中尝试添加该行的值,其值类似于Mazda
。下面是我正在使用的df。
df <- data.frame(month = c("mazda 3", "mazda cx5", "mazda 6","mazda miata","honda civic","honda accord"),
april = c(.1,.2,.3,.3,.4,.5),
may = c(.3,.4,.5,.2,.1,.5),
june = c(.2,.1,.5,.1,.2,.3))
d2<- df %>% mutate(total == (rowsum(df[-1], df[rownames(month) %like% "Mazda"])))
输出应为:
df_out <- data.frame(month = c("mazda 3", "mazda cx5", "mazda 6","mazda miata","honda civic","honda accord","total_mazda"),
april = c(.1,.2,.3,.3,.4,.5,.8),
may = c(.3,.4,.5,.2,.1,.5,1.4),
june = c(.2,.1,.5,.1,.2,.3,.9))
答案 0 :(得分:1)
我们可以获取sum
中数字列的summarise_at
,同时根据“ month”中的“ mazda”子字符串对值进行子集设置,创建“ month”列并与原始数据集绑定
library(tidyverse)
df %>%
summarise_at(2:4, funs(sum(.[str_detect(month, 'mazda')]))) %>%
mutate(month = 'Total') %>%
bind_rows(df, .)
答案 1 :(得分:0)
尝试使用apply(df, 2, ...)
在第一列中使用grepl
遮罩mazda的掩码来循环浏览这些列。
我使用了一些bind_(rows|cols)
技巧来获取正确格式的数据帧。
library(dplyr)
df <- data_frame(month = c("mazda 3", "mazda cx5", "mazda 6","mazda miata","honda civic","honda accord"),
april = c(.1,.2,.3,.3,.4,.5),
may = c(.3,.4,.5,.2,.1,.5),
june = c(.2,.1,.5,.1,.2,.3))
df_out <- bind_rows(
df %>% as_data_frame(),
data_frame(month = "total_mazda") %>%
bind_cols(
apply(df[, 2:ncol(df)],
2,
function(x, y = grepl(".*(m|M)azda.*", df[[1]])) sum(x[y])
) %>%
as.list() %>%
as_data_frame()))