I am trying to write a function to allow the user to input various parameters ("means", "temp", etc.), which are then compiled into a list so that I can work with them. Values from the list are extracted and assigned to objects.
我正在尝试编写一个函数来允许用户输入各种参数(“means”,“temp”等),然后将这些参数编译成一个列表,以便我可以使用它们。提取列表中的值并将其分配给对象。
param <- function(){
parameters <- c("means","variance","temp","gene","add")
x <- list()
for(i in 1:length(parameters)){
z <- readline(paste("Input ", parameters[i], ": ", sep=""))
#store inputs in vector x
x[[z]] <- z
#x[[i]] <- z
#x[i] <- z
} #End of for loop to bring in inputs
#Bind results into a vector
y=c(x[[1]],x[[2]],x[[3]],x[[4]],x[[5]])
#Assign answers to proper variable names so they can be used to index.
means <- y[1]; variance <- y[2]; temp <- y[3]; gene <- y[4]; add <- y[5]
#means <- x[[1]]; variance <- x[[2]]; temp <- x[[3]]; gene <- x[[4]]; add <- x[[5]]
} #End of function to bring in inputs
I want to use the objects to index a larger data frame (strn.x).
我想使用对象索引更大的数据框(strn.x)。
yav=strn.x[NT==temp, means]
I've referenced https://stat.ethz.ch/pipermail/r-help/2008-April/158757.html, to learn how to assign user inputs to objects, and I recognize (based on Retrieving specific values from subsetting a data.table) that part of my problem is the class the objects take on. I've attempted to use "[[" to extract the values without the name, but they still come out as class 'list' or 'data.frame' depending on what I include in the function above.
我引用了https://stat.ethz.ch/pipermail/r-help/2008-April/158757.html,以了解如何将用户输入分配给对象,并且我认识到(基于从子集化中检索特定值) data.table)我的问题的一部分是对象所采用的类。我试图使用“[[”来提取没有名称的值,但它们仍然作为类'list'或'data.frame'出现,具体取决于我在上面的函数中包含的内容。
means
V1
1 FT_mn
class(means)
[1] "data.frame"
I'd like them to take on the form:
我希望他们接受表格:
[1] FT_mn
What do I need to do to get this to work?
我需要做些什么才能让它发挥作用?
1 个解决方案
#1
0
Try unlist() to convert a data.frame or list into a vector.
尝试使用unlist()将data.frame或列表转换为向量。
#1
0
Try unlist() to convert a data.frame or list into a vector.
尝试使用unlist()将data.frame或列表转换为向量。