0

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.

Community
  • 1
  • 1
user2405694
  • 847
  • 2
  • 8
  • 19
  • You can explore it with `debug(distfunc)`. Read the documentation of `debug()` to learn how to handle the debugger. – jogo Apr 20 '16 at 12:46
  • You are calling `distfunc` with vectors of `n` and `m` but expecting them to work as numeric values inside that function. Depending on what you are trying to do, invoking `distfunc` multiple times might be the right answer. – Gopala Apr 20 '16 at 13:07

2 Answers2

1

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)
}
jogo
  • 12,469
  • 11
  • 37
  • 42
user2405694
  • 847
  • 2
  • 8
  • 19
  • `data.frame(X1=10, X2=20, X3=unlist(mapply(':', x$n, x$m)))` is another way to construct the dataframe. Or `data.frame(X1=10, X2=20, X3=unlist(apply(x[-1], 1, function(x) x[1]:x[2])))` – jogo Apr 20 '16 at 14:21
  • @jogo I updated my question. It also return error message. – user2405694 Apr 21 '16 at 03:23
  • Does your answer give the desired result for the edited question? – jogo Apr 22 '16 at 13:04
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]))
jogo
  • 12,469
  • 11
  • 37
  • 42