我的数据看起来像这样
best..F1
best..F2
.
.
基本上,我想提取名称以
开头的所有列SELECT
i.observation_id,
SUM(IF(i.value=5,1,0)) / counts.num as 'percent_positive'
FROM intervals i
inner join (
select observation_id, count(1) as num from intervals group by observation_id
) counts on counts.observation_id = i.observation_id
group by i.observation_id
order by i.observation_id
;
有没有办法做到这一点,而无需逐个访问数据并找到它的位置?
答案 0 :(得分:3)
您可以尝试grep
,如下所示:
df[,grep("^best\\.\\.F",names(df))]
答案 1 :(得分:3)
library(dplyr)
df %>% select(starts_with("best..F"))
答案 2 :(得分:2)
我们可以使用startsWith
base R
df[startsWith(names(df), "best..F")]