简体   繁体   English

R:动态创建变量名

[英]R: Dynamically create a variable name

I'm looking to create multiple data frames using a for loop and then stitch them together with merge() . 我正在寻找使用for循环创建多个数据帧,然后使用merge()它们缝合在一起。

I'm able to create my data frames using assign(paste(), blah) . 我可以使用assign(paste(), blah)创建数据框。 But then, in the same for loop, I need to delete the first column of each of these data frames. 但是,然后在同一for循环中,我需要删除每个数据帧的第一列。

Here's the relevant bits of my code: 这是我的代码的相关部分:

for (j in 1:3)
{
    #This is to create each data frame
    #This works
    assign(paste(platform, j, "df", sep = "_"), read.csv(file = paste(masterfilename,    extension, sep = "."), header = FALSE, skip = 1, nrows = 100))

    #This is to delete first column
    #This does not work
    assign(paste(platform, j, "df$V1", sep = "_"), NULL)
}

In the first situation I'm assigning my variables to a data frame, so they inherit that type. 在第一种情况下,我将变量分配给数据框,因此它们会继承该类型。 But in the second situation, I'm assigning it to NULL . 但是在第二种情况下,我将其分配给NULL

Does anyone have any suggestions on how I can work this out? 有人对我如何解决这个问题有任何建议吗? Also, is there a more elegant solution than assign() , which seems to bog down my code? 另外,是否有比assign()更优雅的解决方案,这似乎使我的代码陷入困境? Thanks, 谢谢,

ni

assign can be used to build variable names, but "name$V1" isn't a variable name. assign可以用来构建变量名,但是“ name $ V1”不是变量名。 The $ is an operator in R so you're trying to build a function call and you can't do that with assign . $是R中的运算符,因此您正在尝试构建函数调用,而您不能使用assign做到这一点。 In fact, in this case it's best to avoid assign completely. 实际上,在这种情况下,最好避免完全assign You con't need to create a bunch of different variables. 您无需创建一堆不同的变量。 If you data.frames are related, just keep them in a list. 如果data.frame是相关的,只需将它们放在列表中即可。

mydfs <- lapply(1:3, function(j) {
    df<- read.csv(file = paste(masterfilename, extension, sep = "."), 
        header = FALSE, skip = 1, nrows = 100))
    df$V1<-NULL
    df
})

Now you can access them with mydfs[[1]] , mydfs[[2]] , etc. And you can run functions overall data.sets with any of the *apply family of functions. 现在,您可以使用mydfs[[1]]mydfs[[2]]等访问它们。并且您可以使用*apply函数族中的任何一个运行函数总体data.sets。

As @joran pointed out in his comment, the proper way of doing this would be using a list. 正如@joran在他的评论中指出的那样,执行此操作的正确方法是使用列表。 But if you want to stick to assign you can replace your second statement with 但是,如果您想坚持assign ,可以将第二条语句替换为

assign(paste(platform, j, "df", sep = "_"), 
    get(paste(platform, j, "df", sep = "_"))[
        2:length(get(paste(platform, j, "df", sep = "_")))]

If you wanted to use a list instead, your code to read the data frames would look like 如果您想使用列表,则读取数据框的代码将如下所示:

dfs <- replicate(3,
    read.csv(file = paste(masterfilename, extension, sep = "."),
        header = FALSE, skip = 1, nrows = 100), simplify = FALSE)

Note you can use replicate because your call to read.csv does not depend on j in the loop. 请注意,可以使用replicate因为对read.csv的调用不依赖于循环中的j Then you can remove the first column of each 然后,您可以删除每个广告的第一列

dfs <- lapply(dfs, function(d) d[-1])

Or, combining everything in one command 或者,将所有内容合并到一个命令中

dfs <- replicate(3,
    read.csv(file = paste(masterfilename, extension, sep = "."),
        header = FALSE, skip = 1, nrows = 100)[-1], simplify = FALSE)

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

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