读入文件并将字段设置为R中的变量
我试图读入一个文件,并让R将每个数据对设置为一个环境变量 我有一个来自web的输入文件,其中包含以下数据。它具有需要读入的数字和字符变量。这就是我被挂断的地方,因为我似乎只能将它们作为一个或另一个引入,这使得assign或do.call错误出现在被分类为数字的字符中,反之亦然 有什么想法吗读入文件并将字段设置为R中的变量,r,environment-variables,assign,do.call,R,Environment Variables,Assign,Do.call,我试图读入一个文件,并让R将每个数据对设置为一个环境变量 我有一个来自web的输入文件,其中包含以下数据。它具有需要读入的数字和字符变量。这就是我被挂断的地方,因为我似乎只能将它们作为一个或另一个引入,这使得assign或do.call错误出现在被分类为数字的字符中,反之亦然 有什么想法吗 input file "input": user <- "johndoe", date <- "1-30-2019", run <- 1, scoring <- "default",
input file "input":
user <- "johndoe", date <- "1-30-2019", run <- 1, scoring <- "default",
high_cutoff <- 70, low_cutoff <- 15, bg1 <- 12, bg2 <- 12, bg3 <- 12,
group_cut <- 60, stbg <- "no", factor <- 25, rest <- 2,
loo_enable <- "no"
wd <- getwd()
phpvars <- as.data.frame(t(read.table(paste0(wd, "/input"), sep = ",")))
#attempt 1 - reads everything in as character
for (i in phpvars$V1){
x<- t(as.data.frame(strsplit(i," <- ")))
assign(x[1,1],(x[1,2]))
}
#attempt 2 - tries to detect type of each data point as it comes in, and
#assign as. (numeric or character)
for (i in phpvars$V1){
x<- t(as.data.frame(strsplit(i," <- ")))
type <- typeof(x[1,2])
d <- paste0("as.", type)
e <- x[1,2]
assign(x[1,1],paste(d,(e)))
}
#alternate to assign - has same problems though
do.call("<-",list(x[1,1],x[1,2]))
输入文件“输入”:
用户您已经有了一个很好的引用输入。在使用read.table()
函数时,通过指定输入字符串用quote=“
参数引用来利用这一点:
wd <- getwd()
phpvars <- as.data.frame( t( read.table( paste0(wd, "/input") ,
sep = ",",
quote = "" ) ) )
如果您的输入文件没有被引用,您可以始终使用grepl(pattern=“[0-9]+”,x=您的_字符串)
在函数中拆分每一行后,评估您的值是字符类还是数字类
wd <- getwd()
phpvars <- as.data.frame( t( read.table( paste0(wd, "/input") ,
sep = ",",
quote = "" ) ) )
for(i in phpvars$V1){
eval( parse( text = as.character(i) ) )
}