[英]R language assign character value as data set name
I need to process hundreds of data frames through the same set of code. 我需要通过同一组代码处理数百个数据帧。 For illustrative purposes, I create 3 data frames:
出于说明目的,我创建了3个数据框:
ds.1 = as.data.frame(matrix(c(1, 0.15, 0.15, 0.15,
0.2, 1, 0.15, 0.15,
0.2, .15, 1, 0.15,
0.2, 0.15, 0.15, 1), nrow=4, ncol=4))
ds.2 = as.data.frame(matrix(c(1, 0.25, 0.25, 0.25,
0.2, 1, 0.25, 0.25,
0.2, .25, 1, 0.25,
0.2, 0.25, 0.25, 1), nrow=4, ncol=4))
ds.3 = as.data.frame(matrix(c(1, 0.50, 0.50, 0.50,
0.2, 1, 0.50, 0.50,
0.2, .50, 1, 0.50,
0.2, 0.50, 0.50, 1), nrow=4, ncol=4))
I then assign the data frame names to a vector. 然后,我将数据帧名称分配给向量。
ds.vector <- c("ds.1", "ds.2", "ds.3") #create a vector of data set names
I count the number of data frames in the vector 我计算向量中的数据帧数
ds.in.vector <- length(ds.vector) #count the number of elements in vector
I loop through the vector, and seek to assign the data frame name to a data frame called ds. 我遍历向量,并试图将数据帧名称分配给名为ds的数据帧。 I would then run the code on ds.
然后,我将在ds上运行代码。
for (i in 1:ds.in.vector)
{
ds <- ds.vector[i] #copy each of the data sets into ds
#There would be a bunch of code here. For this example,
# I will just try to print
print(ds)
}
The line: ds <- ds.vector[i] does not copy the data frame whose name is in the vector, into ds. ds <-ds.vector [i]行不会将名称在矢量中的数据帧复制到ds中。 Rather, it copies the character string of the vector into ds.
而是将向量的字符串复制到ds中。
These data.frames belong into a list. 这些data.frame属于一个列表。 You should assign them into one when you create them.
创建它们时,应将它们分配为一个。 Having hundreds of data.frames in your global environment is just crazy and really not practical.
在您的全球环境中拥有数百个data.frames简直是疯狂的,实际上是不切实际的。
ds.list <- list(
ds.1 = as.data.frame(matrix(c(1, 0.15, 0.15, 0.15,
0.2, 1, 0.15, 0.15,
0.2, .15, 1, 0.15,
0.2, 0.15, 0.15, 1), nrow=4, ncol=4)),
ds.2 = as.data.frame(matrix(c(1, 0.25, 0.25, 0.25,
0.2, 1, 0.25, 0.25,
0.2, .25, 1, 0.25,
0.2, 0.25, 0.25, 1), nrow=4, ncol=4)),
ds.3 = as.data.frame(matrix(c(1, 0.50, 0.50, 0.50,
0.2, 1, 0.50, 0.50,
0.2, .50, 1, 0.50,
0.2, 0.50, 0.50, 1), nrow=4, ncol=4))
)
ds.vector <- c("ds.1", "ds.2", "ds.3")
for (i in seq_along(ds.vector)) {
ds <- ds.list[ds.vector[i]] #copy each of the data sets into ds
#There would be a bunch of code here. For this example,
# I will just try to print
print(ds)
}
Of course, if they all have the same structure, you can combine them into one data structure: 当然,如果它们都具有相同的结构,则可以将它们组合为一个数据结构:
library(data.table)
DT <- rbindlist(ds.list, idcol=TRUE)
DT[, print(.SD), by = .id]
one way to do it (although not very efficient) is the following: 一种方法(虽然不是很有效)是以下方法:
ds.cumulated <- get(ds.vector[1])
for (i in 2:ds.in.vector)
{
ds.i <- get(ds.vector[i])
ds.cumulated <- rbind(ds.cumulated, ds.i)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.