在dataframe中添加列值,并在标题中使用常用字符

在dataframe中添加列值,并在标题中使用常用字符,r,dataframe,R,Dataframe,我有一个带有多列的R数据帧。 我打算通过根据标题中存在的一些常见字符将一些列添加到一起来进行一些数据清理 简单例子 df date go_pax full_pax plus_pax 2019-03-11 7 23 14 2019-03-12 11 5 6 2019-03-13 20

我有一个带有多列的
R数据帧。
我打算通过根据标题中存在的一些常见字符将一些列添加到一起来进行一些数据清理

简单例子

df

date               go_pax       full_pax       plus_pax
2019-03-11           7            23             14
2019-03-12           11            5              6
2019-03-13           20            4              37
预期输出:在新的“预订”列中添加带有字符
pax
的所有列

我不是在用简单的

 df_demand <- data.frame("date" = df$date, "bookings" = df$gO_pax +  df$full_pax + df$pLUS_pax)
我还没有找到有效的语法

编辑:

我不会使用数字来指定列。我拥有的全部数据有20多列

最终编辑

基于所有的好答案,这对我来说是有效的

pax <- grep("pax", names(df))
df_demand <- data.frame("date" = df$date, "bookings" = rowSums(df[pax]))

pax我们可以获得除第一列之外的列的
行和
,并使用原始数据集的第一列创建一个
data.frame

data.frame(df1[1], bookings = rowSums(df1[-1]))
#         date bookings
#1 2019-03-11       44
#2 2019-03-12       22
#3 2019-03-13       61

如果我们需要指定包含“pax”的列

nm1 <- grep("pax", names(df1))
data.frame(df1[1], bookings = rowSums(df1[nm1]))

如果我们需要一个
tidyverse
,选项(无需再次重塑),
selecg
将“pax”作为列名子字符串的列,然后在
reduce
中使用
+
创建“预订”

library(tidvyerse)
df1 %>% 
   transmute(date, bookings = select(., matches("pax")) %>% 
                 reduce(`+`))
#        date bookings
#1 2019-03-11       44
#2 2019-03-12       22
#3 2019-03-13       61

或者另一个选项是
rowSums
,我们首先在这里发布

df1 %>%
   transmute(date, bookings = rowSums(.[nm1]))
#        date bookings
#1 2019-03-11       44
#2 2019-03-12       22
#3 2019-03-13       61
在这里,我们不需要任何重塑,它应该是快速的

数据
df1使用
tidyverse
您可以尝试:

df %>%
 select(date, contains("_pax")) %>%
 gather(var, val, -date) %>%
 group_by(date) %>%
 summarise(bookings = sum(val))

    date       bookings
  <chr>         <int>
1 2019-03-11       44
2 2019-03-12       22
3 2019-03-13       61

使用
sapply

df = data.frame(df[,1],
                "bookings" = sapply(1:nrow(df), function(x) sum(df[x, grep('pax', colnames(df))])))

我认为如果你在做
sum
的话,一个更好的选择是
apply(df[grep('pax',names(df))],1,sum)
是的,那会更好。
library(tidvyerse)
df1 %>% 
   transmute(date, bookings = select(., matches("pax")) %>% 
                 reduce(`+`))
#        date bookings
#1 2019-03-11       44
#2 2019-03-12       22
#3 2019-03-13       61
df1 %>%
   transmute(date, bookings = rowSums(.[nm1]))
#        date bookings
#1 2019-03-11       44
#2 2019-03-12       22
#3 2019-03-13       61
df1 <- structure(list(date = c("2019-03-11", "2019-03-12", "2019-03-13"
 ), go_pax = c(7L, 11L, 20L), full_pax = c(23L, 5L, 4L), plus_pax = c(14L, 
 6L, 37L)), class = "data.frame", row.names = c(NA, -3L))
df %>%
 select(date, contains("_pax")) %>%
 gather(var, val, -date) %>%
 group_by(date) %>%
 summarise(bookings = sum(val))

    date       bookings
  <chr>         <int>
1 2019-03-11       44
2 2019-03-12       22
3 2019-03-13       61
df %>%
 select(date, contains("_pax")) %>%
 transmute(date = date,
           bookings = rowSums(.[2:length(.)]))
df = data.frame(df[,1],
                "bookings" = sapply(1:nrow(df), function(x) sum(df[x, grep('pax', colnames(df))])))