以r的特定顺序创建变量名

问题描述 投票:0回答:1

我想获得一些具有特定顺序的变量名,像这样

I0.n,I0.man,I0.woman,I0.low65,I0.up65

I1.n,I1.man,I1.woman,I1.low65,I1.up65

... ... ...]

[[I99.n,I99.man,I99.woman,I99.low65,I99.up65

我尝试了一种方法但是失败了

num <- sprintf('%02d',0:99) index <- c('n', 'man', 'woman', 'low65', 'up65') vars <- paste0('I', num, '.', index)

是否有一种有效的方法来创建具有特定顺序的变量名?

任何帮助将不胜感激!

r dplyr tidyverse paste
1个回答
0
投票
@MrFlick所建议的,一种简单而紧凑的方法是使用以下代码:

data <- with(expand.grid(index=index, num=num), paste0('I', num, '.', index)) > data [1] "I00.n" "I00.man" "I00.woman" "I00.low65" "I00.up65" "I01.n" "I01.man" "I01.woman" [9] "I01.low65" "I01.up65" "I02.n" "I02.man" "I02.woman" "I02.low65" "I02.up65" "I03.n" ... [497] "I99.man" "I99.woman" "I99.low65" "I99.up65"

希望这会有所帮助。
© www.soinside.com 2019 - 2024. All rights reserved.