An error about vectorization in R

2019-09-16 15:56发布

问题:

My R code is as follows. The main task is to calculate the row number of repetitions.

library(plyr)
data<-data.frame(1,2,3);
x <- read.table(text = "ID1    ID2    n    m
13    156   12   15
94    187   14   16
66    297   41   48
29    89    42   49
78    79    51   79", header= TRUE)

distfunc <- function(data,ID1,ID2,n,m){
X1<-ID1; ################
X2<-ID2; ################
X3<-unlist(mapply(':', n, m));
data<-rbind(data,data.frame(X1,X2,X3));
return(data);
}

data<-distfunc(data,x$ID1, x$ID2,x$n, x$m)

data<-data[-1,]

    plyr::count(data, names(data)); ## Calculates the row number of repetitions

The error message I get:

Error in data.frame(X1, X2, X3) : 
  arguments imply differing number of rows: 5, 52

I try to fix it by R Error: “In numerical expression has 19 elements: only the first used”, but it failed and the result is wrong. This probelm is not the same as that probelm.

回答1:

I suppose you want to do:

# library(plyr)
# data<-data.frame(1,2,3);
x <- read.table(header=TRUE, text = 
"ID1    ID2    n    m
  13    156   12   15
  94    187   14   16
  66    297   41   48
  29    89    42   49
  78    79    51   79")

#distfunc <- function(data, ID1, ID2, n, m) {
#  X1 <- ID1 ################
#  X2 <- ID2 ################
#  X3 <- unlist(mapply(':', n, m))
#  data <- rbind(data, data.frame(X1,X2,X3))
#}

#data <- distfunc(data, x$ID1, x$ID2, x$n, x$m)
L <- apply(x, 1, function(x) data.frame(X1=x[1], X2=x[2], X3=x[3]:x[4], row.names=NULL))
data <- L[[1]]
for (i in 2:length(L)) data <- rbind(data, L[[i]])

or with a better readable function in apply():

L <- apply(x, 1, function(r) data.frame(X1=r["ID1"], X2=r["ID2"], X3=r["n"]:r["m"], row.names=NULL))
data <- L[[1]]; for (i in 2:length(L)) data <- rbind(data, L[[i]])

Here is a simpler variant:

data <- data.frame(X1=x$ID1[1], X2=x$ID2[1], X3=x$n[1]:x$m[1])
for (i in 2:nrow(x)) data <- rbind(data, data.frame(X1=x$ID1[i], X2=x$ID2[i], X3=x$n[i]:x$m[i]))


回答2:

I just fixed it.

distfunc <- function(data, ID1, ID2, n, m) {
  X1 <- ID1
  X2 <- ID2
  X3 <- unlist(mapply(':', n, m))
  data <- rbind(data,data.frame(X1, X2, X3))
  return(data)
}