分配给 R 中的变量数据框 [英] Assign to a variable data frame in R

查看:32
本文介绍了分配给 R 中的变量数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一列数据分配给现有数据框中的新列.数据框循环变化,从scores.d 到scores.e.我想要的输出是用 vals 填充 score.X$new.col,其中 X 替换为当前的 dfname.

I am trying to assign a column of data to a new column in an existing data frame. The data frame changes in a loop, from scores.d, to scores.e. My desired output is to have scores.X$new.col populated with the vals, where X is replaced with the current dfname.

dfnames <- c("d","e")
scores.d <- data.frame(x = 1, y = 1:10)
scores.e <- data.frame(x = 2, y = 10:20)
vals <- 60:70

for (i in seq_along(dfnames)){
   assign(get(paste0("scores.",dfnames[i]))$new.col,vals)
}

Error in assign(get(paste0("scores.", dfnames[i]))$new.col, vals) : 
  invalid first argument

这给了我一个错误,因为assign正在寻找一个字符串作为第一个参数,当我需要它包含列名时.简单地将 $new.col 添加到粘贴命令是行不通的(假设 $ 不从字符串转换).

This gives me an error, because assign is looking for a character string as the first argument, when I need it to include the column name. Simply adding $new.col to the paste command doesn't work (assume $ doesn't translate from string).

我是 R 的新手,不知道分配事物的注意事项.我想制作一个数据框列表,然后用 vals 填充每个框,但它不起作用,因为我指定了特定的列,在我的真实数据中,数据框无论如何都是预先存在的,我只是想添加到它们中这里.想法?

I am new to R and don't know the dos and don'ts of assigning things. I thought to make a list of data frames, then populate each with vals, but it didnt work as I am specifying particular columns, in my real data, the data frames are pre-existing anyway, I'm just trying to add to them here. Thoughts?

编辑*@Jason 提供了一个答案,将值分配给一个临时变量,然后将其分配回来.对我的目的来说效果很好,但是我已经尝试过使用字符串列表代替通过 paste0() 创建名称,但它仍然给了我错误.首先,杰森的工作答案:

EDIT* @Jason has provided an answer, by allocating the values to a temporary variable, then assigning it back. Works fine for my purposes, however I had tried it with a list of strings in place of creating the names via paste0(), and it still gave me the error. First, Jason's working answer:

dfnames <- c("d","e")
scores.d <- data.frame(x = 1, y = 1:10)
scores.e <- data.frame(x = 2, y = 11:20)
vals <- 61:70

for (i in dfnames){ #don't need seq_along

    dat<-get(paste0("scores.",i)) #pull up the data 
    dat$new.col<-vals
    assign(paste0('scores.',i),dat) #replace old data frame with new
}

现在用名称列表替换粘贴过程(注意对 seq_along 的更改):

Now with a list of names replacing the paste procedure (note the change to seq_along):

dfnames <- c("d","e")
scores.d <- data.frame(x = 1, y = 1:10)
scores.e <- data.frame(x = 2, y = 11:20)
vals <- 61:70

# for demonstrative purposes only, these were created in a loop in my code
full.dfnames[1] <- "Scores.d"
full.dfnames[2] <- "Scores.e"

for (i in seq_along(dfnames)){ #added seq_along back for the name index

    dat<-get(full.dfnames[i]) #pull up the data 
    dat$new.col<-vals
    assign(full.dfnames[i],dat) #replace old data frame with new
}

>Error in assign(get(paste0("scores.", dfnames[i]))$new.col, vals) : 
      invalid first argument

推荐答案

我相信以下工作虽然可能不像您希望的那样精简.

I believe the following works though might not be as streamlined as you would like.

dfnames <- c("d","e")
scores.d <- data.frame(x = 1, y = 1:10)
scores.e <- data.frame(x = 2, y = 11:20)
vals <- 61:70

for (i in dfnames){ #don't need seq_along

    dat<-get(paste0("scores.",i)) #pull up the data 
    dat$new.col<-vals
    assign(paste0('scores.',i),dat) #replace old data frame with new

}

这篇关于分配给 R 中的变量数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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