I'm new programming and R so I apologize if I'm not being clear enough in this question. I think my problem is two fold. I'll first try to give some context. One, I have a data frame within my data frame:
'data.frame': 27609 obs. of 2 variables:
$ Diff : num 2557 2038 0 30 0 ...
$ freq.:'data.frame': 27609 obs. of 1 variable:
..$ freq: int 85 68 1 31 1 35 1 1 34 42 ...
> head(d.f.)
Diff freq
1 2557 85
2 2038 68
3 0 1
4 30 31
5 0 1
6 1034 35
I think this is causing my subsequent problem with mapply() below where I'd like to apply a function that, in each row, takes the value from one column, divides by a value in another column, then outputs a 1,2,3 or 4 depending on the range of values the quotient lies in.
myFunction = function(a,b) {
interval = (a/b)
ifelse(interval==0, 1 ,
ifelse(interval<1, 2 ,
ifelse(interval<31, 3 , 4)))}
Test = mapply(myFunction, d.f.$Diff, d.f.$freq)
> Test
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 3 3 1 2 1 3
[2,] 4 3 1 2 1 3
[3,] 4 4 1 3 1 4
[4,] 4 4 1 2 1 4
[5,] 4 4 1 3 1 4
[6,] 4 4 1 2 1 3
Above, Test is being run on only the first 6 rows. What ends up happening is Test takes forever to run on the entire d.f. and for some reason ends up outputting a matrix where the only values I'm interested in are in the first row. I'd really appreciate any help to get me to understand what I'm doing wrong. Thanks in advance!