复杂调查R包中复制权重的正则表达式

时间:2016-04-22 12:49:51

标签: r survey

我使用R中的调查包来分析国家犯罪受害调查的数据。 Thomas Lumley的书复杂调查:使用R 进行分析的指南和调查包中的帮助文件使用正则表达式来表示这样的重复权重PERREPWGT[1-160]+。但是,此表达式并不总是标识所有复制权重。如果我将+号更改为?符号,正则表达式工作正常,并标识所有复制权重。一个简短的测试用例如下所示。我对正则表达式不太自信。这是正则表达式的正确更改还是有更可靠的方式来编写模式?

x<-c("V3067", "V3068", "V3069", "V3070", "V3071", "V3072", "V3073", "V3074", "V3075", "V3076", "V3077", "V3078", 
"V3079", "V3080", "WGTPERCY", "V3081", "V3082", "PERREPWGT1", "PERREPWGT2", "PERREPWGT3", "PERREPWGT4", 
"PERREPWGT5", "PERREPWGT6", "PERREPWGT7", "PERREPWGT8", "PERREPWGT9", "PERREPWGT10", "PERREPWGT11", 
"PERREPWGT12", "PERREPWGT13", "PERREPWGT14", "PERREPWGT15", "PERREPWGT16", "PERREPWGT17", "PERREPWGT18", 
"PERREPWGT19", "PERREPWGT20", "PERREPWGT21", "PERREPWGT22", "PERREPWGT23", "PERREPWGT24", "PERREPWGT25", 
"PERREPWGT26", "PERREPWGT27", "PERREPWGT28", "PERREPWGT29", "PERREPWGT30", "PERREPWGT31", "PERREPWGT32", 
"PERREPWGT33", "PERREPWGT34", "PERREPWGT35", "PERREPWGT36", "PERREPWGT37", "PERREPWGT38", "PERREPWGT39", 
"PERREPWGT40", "PERREPWGT41", "PERREPWGT42", "PERREPWGT43", "PERREPWGT44", "PERREPWGT45", "PERREPWGT46", 
"PERREPWGT47", "PERREPWGT48", "PERREPWGT49", "PERREPWGT50", "PERREPWGT51", "PERREPWGT52", "PERREPWGT53", 
"PERREPWGT54", "PERREPWGT55", "PERREPWGT56", "PERREPWGT57", "PERREPWGT58", "PERREPWGT59", "PERREPWGT60", 
"PERREPWGT61", "PERREPWGT62", "PERREPWGT63", "PERREPWGT64", "PERREPWGT65", "PERREPWGT66", "PERREPWGT67", 
"PERREPWGT68", "PERREPWGT69", "PERREPWGT70", "PERREPWGT71", "PERREPWGT72", "PERREPWGT73", "PERREPWGT74", 
"PERREPWGT75", "PERREPWGT76", "PERREPWGT77", "PERREPWGT78", "PERREPWGT79", "PERREPWGT80", "PERREPWGT81", 
"PERREPWGT82", "PERREPWGT83", "PERREPWGT84", "PERREPWGT85", "PERREPWGT86", "PERREPWGT87", "PERREPWGT88", 
"PERREPWGT89", "PERREPWGT90", "PERREPWGT91", "PERREPWGT92", "PERREPWGT93", "PERREPWGT94", "PERREPWGT95", 
"PERREPWGT96", "PERREPWGT97", "PERREPWGT98", "PERREPWGT99", "PERREPWGT100", "PERREPWGT101", "PERREPWGT102", 
"PERREPWGT103", "PERREPWGT104", "PERREPWGT105", "PERREPWGT106", "PERREPWGT107", "PERREPWGT108", "PERREPWGT109", 
"PERREPWGT110", "PERREPWGT111", "PERREPWGT112", "PERREPWGT113", "PERREPWGT114", "PERREPWGT115", "PERREPWGT116", 
"PERREPWGT117", "PERREPWGT118", "PERREPWGT119", "PERREPWGT120", "PERREPWGT121", "PERREPWGT122", "PERREPWGT123", 
"PERREPWGT124", "PERREPWGT125", "PERREPWGT126", "PERREPWGT127", "PERREPWGT128", "PERREPWGT129", "PERREPWGT130", 
"PERREPWGT131", "PERREPWGT132", "PERREPWGT133", "PERREPWGT134", "PERREPWGT135", "PERREPWGT136", "PERREPWGT137", 
"PERREPWGT138", "PERREPWGT139", "PERREPWGT140", "PERREPWGT141", "PERREPWGT142", "PERREPWGT143", "PERREPWGT144", 
"PERREPWGT145", "PERREPWGT146", "PERREPWGT147", "PERREPWGT148", "PERREPWGT149", "PERREPWGT150", "PERREPWGT151", 
"PERREPWGT152", "PERREPWGT153", "PERREPWGT154", "PERREPWGT155", "PERREPWGT156", "PERREPWGT157", "PERREPWGT158", 
"PERREPWGT159", "PERREPWGT160")


#Regular expression consistent with examples in survey package
#Does NOT identify all replicate weight names
grep("PERREPWGT[1-160]+", x)

#Correctly identifies array index 18 through 177
grep("PERREPWGT[1-160]?", x)

2 个答案:

答案 0 :(得分:0)

[1-160]实际上并不是一个有用的正则表达式模式。你想匹配所有单个数字0-9,并允许多个。要执行此操作,请使用grep( "PERREPWGT[0-9]+" , x )

答案 1 :(得分:0)

原始问题所引用的示例使用正则表达式,例如“ WT [1-9] +”,以WT指定任何变量名,后跟至少一位数字1-9。 1-9很重要:在该数据集中,采样权重为WT0,并且选择了正则表达式以不选择WT0。