我有这样的数据:
df_1 <- data.frame(
x = replicate(
n = 10, expr = runif(n = 1000, min = 20, max = 100)
)
)我的代码:
library(dplyr)
df_1 |>
(\(x) cbind(x, r = apply(x[colnames(x = select(x, where(is.numeric) & head(x = everything(x), 2) & starts_with("x.")))], 1, sum, na.rm = T)))()我试着用[代替colnames,但不起作用。我想转换这个部分(同时,作为上面制作的一个dplyr::select结构):
[colnames(x = select(x, where(is.numeric) & head(x = everything(x), 2) & starts_with("x.")))]设置为基本R。
发布于 2021-12-02 23:20:04
只需这样做:
transform(df_1, r = x.1 + x.2)或者甚至是:
cbind(df_1, r = rowSums(df_1[1:2]))或者甚至是:
cbind(df_1, r = df_1[1] + df_1[2])https://stackoverflow.com/questions/70207289
复制相似问题