新手刚学R语言遇到了一些问题,运行之后出现了这些报错,请问应该怎么解决?求大神指导,非常感谢!
> mulfa <- function(x){
+ n <- num_q[x]
+ A <- matrix(data=rep(0,nrow(data1)*n),nrow=nrow(data1),ncol=n)
+
+ for(i in 1:nrow(data1)){
+ if(is.na(vv[i,x])){
+ A[i,] = rep(0,n)
+ }else{
+ vv[i,x] <- gsub("|","",vv[i,x],fixed=T)
+ a <- unlist(strsplit(vv[i,x],split=""))
+ a <- as.numeric(a)
+ for(j in 1:length(a)){
+ A[i,a[j]+1] <- 1
+ }
+ }
+ }
+
+ name <- c()
+ B <- c("q500","q510","q520","q530","q540","q560","q580","q110")
+ for(m in 1:n){
+ name[m] <- paste(B[x],".",as.character(m))
+ }
+ colnames(A) <- name
+ return(as.data.frame(A))
+ }
> Q500 <- mulfa(1)
Error in `[<-`(`*tmp*`, i, a[j] + 1, value = 1) : subscript out of bounds
> Q510 <- mulfa(2)
Error in `[<-`(`*tmp*`, i, a[j] + 1, value = 1) : subscript out of bounds
> Q520 <- mulfa(3)
> Q530 <- mulfa(4)
Warning messages:
1: In mulfa(4) : NAs introduced by coercion
2: In mulfa(4) : NAs introduced by coercion