[英]R - creating data frames inside a for loop
I have hardcoded this: 我已经对此进行了硬编码:
s79t5 <- read.csv("filename1.csv", header = TRUE)
s81t2 <- read.csv("filename2.csv", header = TRUE)
etc.
subsets79t5 <- subset(s79t5, Tags!='')
subsets81t2 <- subset(s81t2, Tags!='')
...
subsets100t5 <- subset(s100t5, Tags!='')
now i need to softcode it. 现在我需要对其进行软编码。 i am almost there:
我几乎在那里:
sessions <- c('s79t5', 's81t2', 's88t2', 's90t3', 's96t3', 's98t4', 's100t5')
for (i in 1:length(sessions)) {
jFileName <- c(as.character(sessions[i]))
j <- data.frame(jFileName)
subset <- subset(j, j$Tags!='')
assign(paste("subset", jFileName, sep = ""), data.frame(subset))
}
Just throwing an answer here to close this question. 只需在此处回答即可结束该问题。 Discussion was in the comments.
评论中有讨论。
You need the get
function in your line: j <- data.frame(jFileName)
您需要在行中使用
get
函数: j <- data.frame(jFileName)
It should be: j <- as.data.frame(get(jFileName))
它应该是:
j <- as.data.frame(get(jFileName))
The get function looks in your existing objects for the string character you gave it (in this case, jFileName
) and returns that object. get函数在现有对象中查找您给它的字符串字符(在本例中为
jFileName
),然后返回该对象。 I then make sure it is a data frame with as.data.frame
. 然后,我确保它是带有
as.data.frame
的数据框。
Previously you were essentially telling R to make a data frame out of a character string. 以前,您实际上是在告诉R从字符串中制作数据帧。 With
get
you are now referencing your actual dataset. 使用
get
,现在可以引用您的实际数据集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.