[R] 中的简单 [SAS] 宏,怎么样? [英] simple [SAS] macro in [R], how?

查看:16
本文介绍了[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.frames,则将它们保存在列表中.有很多方法可以将许多文件读入列表(请参阅此处).所以说你有一个 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天全站免登陆