如果列名称与模式匹配,我想使某些列中的所有值为负(乘以-1).
我的数据:
df <- structure(list(forest_closed_start = c(3.87, 1.134, 0, 1.8, 2.43,
40.752, 22.95, 9.432, 1.89, 1.53), forest_semi_closed_start = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), shrub_start = c(0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L), forest_closed_end = c(1.935, 0, 0, 1.8,
2.43, 0, 22.95, 0, 0, 0), forest_semi_closed_end = c(0, 0, 0,
0, 0, 8.1504, 0, 0, 0, 1.53), shrub_end = c(0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L)), row.names = c(NA, 10L), class = "data.frame", .Names = c("forest_closed_start",
"forest_semi_closed_start", "shrub_start", "forest_closed_end",
"forest_semi_closed_end", "shrub_end"))
Run Code Online (Sandbox Code Playgroud)
好像:
> df
forest_closed_start forest_semi_closed_start shrub_start forest_closed_end forest_semi_closed_end shrub_end
1 3.870 0 0 1.935 0.0000 0
2 1.134 0 0 0.000 0.0000 0
3 0.000 0 0 0.000 0.0000 0
4 1.800 0 0 1.800 0.0000 0
5 2.430 0 0 2.430 0.0000 0
6 40.752 0 0 0.000 8.1504 0
7 22.950 0 0 22.950 0.0000 0
8 9.432 0 0 0.000 0.0000 0
9 1.890 0 0 0.000 0.0000 0
10 1.530 0 0 0.000 1.5300 0
Run Code Online (Sandbox Code Playgroud)
我想将包含'start'的所有列转换为负数.
谢谢
-al
编辑1:
我还想创建两个新列(df$start和df$end),它们分别是包含'start'和'end'的所有列的总和.
要么
df[,grep("start", names(df))] <- df[,grep("start", names(df))] * -1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
108 次 |
| 最近记录: |