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

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

问题描述

我正在尝试将数据列分配给现有数据帧中的新列.数据帧从scores.d到scores.e循环变化.我想要的输出是用vals填充scores.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的新手,不知道分配事情的优缺点.我以为要列出一个数据帧列表,然后用val填充每个数据帧,但是当我指定特定的列时它没有用,在我的真实数据中,这些数据帧无论如何都是预先存在的,我只是想添加到它们中这里.有什么想法吗?

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天全站免登陆