2017-10-09 57 views

回答

0

我只看到了「月」,如果是「九」在str_detect功能

library(stringr) 

df[,str_detect(names(df),'Sep')]=df[,str_detect(names(df),'Sep')]*0.5 
df 
    Species Aug.16 Sep.16 Aug.17 Sep.17 
1  Cat 740 34.5 758 221.5 
2  Dog 783 279.5 230 71.5 
3 Lizard 294 482.5 718 469.0 
+0

對不起,我更新了它。感謝您指出了這一點。 –

+0

@ R.Baratheon是否解決了您的問題? – Wen

0

您可以爲「九月」做這種改變9至9月喜歡以下內容:

library(dplyr) 
col_names <- names(df %>% select(contains("Sep"))) 
df[,col_names] = df[,col_names]*0.5 
2

在基R:

df1[grepl("Sep",names(df1))] <- 0.5* df1[grepl("Sep",names(df1))] 
0

甲dplyr選項:

library(dplyr) 

df <- structure(list(Species = c("Cat", "Dog", "Lizard"), 
        `Aug-16` = c(740L, 783L, 294L), 
        `Sep-16` = c(69L, 559L, 965L), 
        `Aug-17` = c(758L, 230L, 718L), 
        `Sep-17` = c(443L, 143L, 938L)), 
       .Names = c("Species", "Aug-16", "Sep-16", "Aug-17", "Sep-17"), 
       class = "data.frame", row.names = c(NA, -3L)) 

df2 <- df %>% mutate_at(vars(starts_with('Sep')), funs(. * .5)) 

df2 
#> Species Aug-16 Sep-16 Aug-17 Sep-17 
#> 1  Cat 740 34.5 758 221.5 
#> 2  Dog 783 279.5 230 71.5 
#> 3 Lizard 294 482.5 718 469.0 
相關問題