我有一个将用户输入(通过readline()
)转换为变量列表的函数。我绝对希望R处理2种情况:
用户提供的数字范围将占用列表中的一个插槽
用户提供了一系列数字,每个数字将在列表中占据一个单独的位置。
代码如下:
library(stringr)
translatingVar<-function(answer,varList){# 'answer' is the user's input and 'varList' is a name that will store user's input
var1<-NULL
bool<-0
y<-0
varA<-list()
answer<-strsplit(answer,",",fixed = TRUE)# splitting a string of input into separate intended inputs
for(i in 1:length(answer[[1]])){# 'for' loop to process each input
answer[[1]][i]<-str_remove_all(answer[[1]][i],"[ ]")# removing any empty spaces
if(is.na(as.numeric(answer[[1]][i]))){#checking if input can be translated to numeric format straight away
if(!length(grep("all",answer[[1]][i],fixed=TRUE))>0){#Tried to create a variable to separate 2 situations.
bool<-1
}
#Here are bunch of 'if' functions that process different scenarios
if(!length(grep(":",answer[[1]][i],fixed=TRUE))>0){
varA[[i+y]]<-answer[[1]][i]
}else if(grep(":",answer[[1]][i],fixed=TRUE)&bool==1){
varA[[i+y]]<-c(as.numeric(strsplit(answer[[1]][i],":",fixed=TRUE)[[1]][1]):as.numeric(strsplit(answer[[1]][i],":",fixed=TRUE)[[1]][2]))
}else if(grep("all",answer[[1]][i],fixed=TRUE)){
answer[[1]][i]<-str_remove_all(answer[[1]][i],"[all ]")
var1<-as.list(c(as.numeric(strsplit(answer[[1]][i],":",fixed=TRUE)[[1]][1]):as.numeric(strsplit(answer[[1]][i],":",fixed=TRUE)[[1]][2])))
varA<-append(varA,var1)
y<-length(var1)
}else{
varA[[i+y]]<-answer[[1]][i]
}
}else{
varA[[i+y]]<-as.numeric(answer[[1]][i])
}
}
assign(varList,varA,envir = .GlobalEnv)
}
ss<-readline()
translatingVar(ss,"Variables")
当前,我收到以下错误:
Error in as.numeric(strsplit(answer[[1]][i], ":", fixed = TRUE)[[1]][1]):as.numeric(strsplit(answer[[1]][i], :
NA/NaN argument
我认为这意味着answer[[1][i]
为空,因此无法采取进一步的措施。我试图移动'if'函数,并使用额外的变量来帮助R更好地识别不同的场景,但效果不佳。
所需结果:
输入-“ 1,2,3:5,全部6:8,9”
输出-列表(1,2,c(3:5),6,7,8,9)
答案 0 :(得分:0)
仔细观察,我注意到bool
变量位于for循环之外,这意味着它不会在每次迭代时都被覆盖。
以下更正的代码:
translatingVar<-function(answer,varList){
var1<-NULL
y<-0
varA<-list()
answer<-strsplit(answer,",",fixed = TRUE)
for(i in 1:length(answer[[1]])){
answer[[1]][i]<-str_remove_all(answer[[1]][i],"[ ]")
bool<-0
if(is.na(as.numeric(answer[[1]][i]))){
if(!length(grep("all",answer[[1]][i],fixed=TRUE))>0){
bool<-1
}
if(!length(grep(":",answer[[1]][i],fixed=TRUE))>0){
varA[[i+y]]<-answer[[1]][i]
}else if(grep("all",answer[[1]][i],fixed=TRUE)&&bool==0){
answer[[1]][i]<-str_remove_all(answer[[1]][i],"[all ]")
var1<-as.list(c(as.numeric(strsplit(answer[[1]][i],":",fixed=TRUE)[[1]][1]):as.numeric(strsplit(answer[[1]][i],":",fixed=TRUE)[[1]][2])))
varA<-append(varA,var1)
y<-length(var1)
}else if(grep(":",answer[[1]][i],fixed=TRUE)&&bool==1){
varA[[i+y]]<-c(as.numeric(strsplit(answer[[1]][i],":",fixed=TRUE)[[1]][1]):as.numeric(strsplit(answer[[1]][i],":",fixed=TRUE)[[1]][2]))
}else{
varA[[i+y]]<-answer[[1]][i]
}
}else{
varA[[i+y]]<-as.numeric(answer[[1]][i])
}
}
if(!length(which(lapply(varA,is.null)==TRUE))>0){
assign(varList,varA,envir = .GlobalEnv)
}
assign(varList,varA[-which(lapply(varA,is.null)==TRUE)],envir = .GlobalEnv)
}