[R]中的简单[SAS]宏,如何? [英] simple [SAS] macro in [R], how?

查看:66
本文介绍了[R]中的简单[SAS]宏,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在工作库中有一个名为example1,example2,example3,example4的数据集,其变量为SEX(1或2)通过使用SAS中的MACRO,我将名为exampleS1,exampleS2,exampleS3,exampleS4的数据集限制为SEX = 1

就像这样.

I have datasets called example1,example2,example3,example4 of which variable is SEX(1 or 2) in working library and I've made datasets called exampleS1,exampleS2,exampleS3,exampleS4 restricted to SEX=1 by using MACRO in SAS

%macro ms(var=);
data exampleS&var.;

set example&var.; IF SEX=1;
run; 
%mend ms;%ms(var=1);%ms(var=2);%ms(var=3);%ms(var=4);


现在,我想在R中完成这项工作对我来说,用R做到这一点并不容易.我该怎么做?(假设example1,example2,example3,example4是data.frames)


Now, I want to do this job in R It's bit not easy to do this in R to me. How can I do it? (assuming example1,example2, example3,example4 are data.frames)

谢谢.

推荐答案

在名称中包含带有数字索引的变量是SAS要做的事情,而根本不像R.如果在R中具有相关的data.frame,则将它们保留在列表中.有很多方法可以将许多文件读入列表(请参见此处).假设您有一个data.frames列表

Having variables with numeric index in the name is a very SAS thing to do, and not at all very R like. If you have related data.frames, in R, you keep them in a list. There are many ways to read in many files into a list (see here). So say you have a list of data.frames

examples <- list(
    data.frame(id=1:3, SEX=c(1,2,1)),
    data.frame(id=4:6, SEX=c(1,1,2)),
    data.frame(id=7:9, SEX=c(2,2,1))
)

然后您可以使用来获取所有的SEX = 1值

Then you can get all the SEX=1 values with

exampleS <- lapply(examples, subset, SEX==1)

,您可以通过

exampleS[[1]]
exampleS[[2]]
exampleS[[3]]

这篇关于[R]中的简单[SAS]宏,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆