向R中的数据帧添加多个动态变量

向R中的数据帧添加多个动态变量,r,function,loops,dplyr,mutate,R,Function,Loops,Dplyr,Mutate,我想向数据帧添加一系列变量: patent <- c(1,2,2) temp1 <- c(TRUE,FALSE,FALSE) temp2 <- c(FALSE,TRUE,TRUE) df <- data.frame(patent,temp1,temp2) df patent temp1 temp2 1 TRUE FALSE 2 FALSE TRUE 2 FALSE TRUE 使用dplyr您可以 library(dp

我想向数据帧添加一系列变量:

patent <- c(1,2,2)
temp1 <- c(TRUE,FALSE,FALSE)
temp2 <- c(FALSE,TRUE,TRUE)
df <- data.frame(patent,temp1,temp2)
df

  patent temp1 temp2
       1  TRUE FALSE
       2 FALSE  TRUE
       2 FALSE  TRUE

使用
dplyr
您可以

library(dplyr)
df %>% group_by(patent) %>%
  mutate(new1=sum(temp1), new2=sum(temp2))

#   patent temp1 temp2  new1  new2
#    <dbl> <lgl> <lgl> <int> <int>
# 1      1  TRUE FALSE     1     0
# 2      2 FALSE  TRUE     0     2
# 3      2 FALSE  TRUE     0     2
库(dplyr)
df%%>%组由(专利)%%>%
变异(new1=sum(temp1),new2=sum(temp2))
#专利temp1 temp2 new1 new2
#        
#1真1假10
#2错误-正确0 2
#3 2假对0 2

使用
dplyr
您可以

library(dplyr)
df %>% group_by(patent) %>%
  mutate(new1=sum(temp1), new2=sum(temp2))

#   patent temp1 temp2  new1  new2
#    <dbl> <lgl> <lgl> <int> <int>
# 1      1  TRUE FALSE     1     0
# 2      2 FALSE  TRUE     0     2
# 3      2 FALSE  TRUE     0     2
库(dplyr)
df%%>%组由(专利)%%>%
变异(new1=sum(temp1),new2=sum(temp2))
#专利temp1 temp2 new1 new2
#        
#1真1假10
#2错误-正确0 2
#3 2假对0 2

您可以在不单独指定每列的情况下尝试以下操作

library(dplyr)
df %>%
  group_by(patent) %>%
  mutate_at(vars(contains("temp")), sum) %>%
  ungroup() %>%
  select(-patent) %>%
  setNames(paste0("new", seq_len(ncol(df)-1))) %>%
  cbind(df, .)

  # patent temp1 temp2 new1 new2
# 1      1  TRUE FALSE    1    0
# 2      2 FALSE  TRUE    0    2
# 3      2 FALSE  TRUE    0    2

您可以在不单独指定每列的情况下尝试以下操作

library(dplyr)
df %>%
  group_by(patent) %>%
  mutate_at(vars(contains("temp")), sum) %>%
  ungroup() %>%
  select(-patent) %>%
  setNames(paste0("new", seq_len(ncol(df)-1))) %>%
  cbind(df, .)

  # patent temp1 temp2 new1 new2
# 1      1  TRUE FALSE    1    0
# 2      2 FALSE  TRUE    0    2
# 3      2 FALSE  TRUE    0    2
另一个解决方案。我们可以首先使用
summary_all(funs(sum(.))
计算每列
专利
的总和,然后使用
left_join
将结果加入原始数据框。不需要指定单个列名

library(dplyr)

df2 <- df %>%
  group_by(patent) %>%
  summarise_all(funs(sum(.))) %>%
  setNames(sub("temp", "new", names(.))) %>%
  left_join(df, ., by = "patent")
df2
#   patent temp1 temp2 new1 new2
# 1      1  TRUE FALSE    1    0
# 2      2 FALSE  TRUE    0    2
# 3      2 FALSE  TRUE    0    2
如果列名需要与示例完全相同,我们可以执行以下操作

df2 <- df %>%
  group_by(patent) %>%
  mutate_all(funs("sum" = sum(.))) %>%
  ungroup() %>%
  setNames(ifelse(grepl("_sum$", names(.)), sub("temp", "new", names(.)), names(.))) %>%
  setNames(sub("_sum$", "", names(.)))
df2
# # A tibble: 3 x 5
#   patent temp1 temp2  new1  new2
#    <dbl> <lgl> <lgl> <int> <int>
# 1   1.00 T     F         1     0
# 2   2.00 F     T         0     2
# 3   2.00 F     T         0     2
df2%
组别(专利)%>%
突变所有(funs(“sum”=sum(.))%>%
解组()%>%
集合名称(如果其他(grepl(“\u sum$”),名称(.)),子(“临时”,“新”,名称(.)),名称(.))%>%
集合名称(子(“\u sum$”,“”,名称())
df2
##tibble:3 x 5
#专利temp1 temp2 new1 new2
#        
#1.00 T F 10
#2.00英尺0.2英尺
#3.2.00英尺0.2英尺
另一种解决方案。我们可以首先使用
summary_all(funs(sum(.))
计算每列
专利
的总和,然后使用
left_join
将结果加入原始数据框。不需要指定单个列名

library(dplyr)

df2 <- df %>%
  group_by(patent) %>%
  summarise_all(funs(sum(.))) %>%
  setNames(sub("temp", "new", names(.))) %>%
  left_join(df, ., by = "patent")
df2
#   patent temp1 temp2 new1 new2
# 1      1  TRUE FALSE    1    0
# 2      2 FALSE  TRUE    0    2
# 3      2 FALSE  TRUE    0    2
如果列名需要与示例完全相同,我们可以执行以下操作

df2 <- df %>%
  group_by(patent) %>%
  mutate_all(funs("sum" = sum(.))) %>%
  ungroup() %>%
  setNames(ifelse(grepl("_sum$", names(.)), sub("temp", "new", names(.)), names(.))) %>%
  setNames(sub("_sum$", "", names(.)))
df2
# # A tibble: 3 x 5
#   patent temp1 temp2  new1  new2
#    <dbl> <lgl> <lgl> <int> <int>
# 1   1.00 T     F         1     0
# 2   2.00 F     T         0     2
# 3   2.00 F     T         0     2
df2%
组别(专利)%>%
突变所有(funs(“sum”=sum(.))%>%
解组()%>%
集合名称(如果其他(grepl(“\u sum$”),名称(.)),子(“临时”,“新”,名称(.)),名称(.))%>%
集合名称(子(“\u sum$”,“”,名称())
df2
##tibble:3 x 5
#专利temp1 temp2 new1 new2
#        
#1.00 T F 10
#2.00英尺0.2英尺
#3.2.00英尺0.2英尺

可能的重复不是该问题的重复(尽管仍然很可能是重复),因为这个问题要求在
专利
分组中求和。可能的重复不是该问题的重复(尽管仍然很可能是重复),因为这个问题要求在
专利
分组中求和。