简体   繁体   English

用循环中的字符替换对象名称

[英]Replacing an object name with characters in a loop

I am new to R from SAS.我是 SAS 的 R 新手。 I would use a global macro variable in SAS to accomplish this but haven't found the means in R yet.我会在 SAS 中使用全局宏变量来完成此操作,但尚未在 R 中找到方法。 I want to figure out how to use a loop, or some other R capability, to simplify my code by replacing an object name, that is character, along with attaching the name to additional text ('.sum').我想弄清楚如何使用循环或其他一些 R 功能,通过替换对象名称(即字符)并将名称附加到附加文本 ('.sum') 来简化我的代码。 If I start with the code below:如果我从下面的代码开始:

RED.sum <- aggregate(y ~ x, data = RED, FUN = "mean")
ORANGE.sum <- aggregate(y ~ x, data = ORANGE, FUN = "mean")
YELLOW.sum <- aggregate(y ~ x, data = YELLOW, FUN = "mean")
GREEN.sum <- aggregate(y ~ x, data = GREEN, FUN = "mean")
BLUE.sum <- aggregate(y ~ x, data = BLUE, FUN = "mean")

What do I use that would simplify to one generic line of code:我使用什么可以简化为一行通用代码:

w.sum <- aggregate(y ~ x, data = w, FUN = "mean")

and cycle through the data names (RED, ORANGE, YELLOW, GREEN, BLUE) assigning the value to 'w'?并循环遍历数据名称(红色、橙色、黄色、绿色、蓝色)并将值分配给“w”?

You don't want to have these as separate variables (See here: keep data out of your variable names ).您不想将这些作为单独的变量(请参阅此处: 将数据排除在变量名称之外)。

One option is to keep them in a list, and apply the same function to each with lapply :一种选择是将它们保存在一个列表中,并使用lapply对每个应用相同的函数:

lst <- list(RED, ORANGE, YELLOW, GREEN, BLUE)

sums <- lapply(lst, function(w) aggregate(y ~ x, data = w, FUN = "mean"))

However, if the datasets are otherwise similar, you should probably instead combine them into one table with a color column.但是,如果数据集在其他方面相似,您可能应该将它们组合成一个带有color列的表。 For example:例如:

combined <- rbind(cbind(RED, color = "Red"),
                  cbind(ORANGE, color = "Orange"),
                  cbind(YELLOW, color = "Yellow"))

aggregate(y ~ x + color, data = combined, FUN = "mean")

An alternative for this last operation (that happens to be a lot faster on large datasets) is to use group_by and summarize from the dplyr package:最后一个操作的替代方法(在大型数据集上恰好要快得多)是使用group_by并从 dplyr 包中进行summarize

library(dplyr)
combined %>%
  group_by(x, color) %>%
  summarize(y = mean(y))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM