AndMan21
AndMan21

Reputation: 533

Performing variable arithmetic operations within a for-loop in R

I have the following sample data

    BR WT   SW   PO
1  4.0  7  2.0  1.0
2 12.0  5  4.0  2.0
3  8.0  7  5.0  8.6
4  9.0  3  5.6  9.0
5  1.0 10  6.0  5.0
6  6.0  2  7.5 12.0
7  7.0  3 10.0 15.0
8  4.5 10 12.0  6.0

What I'm trying to do is first go through each column of data, and within each column:

(a) rank each value with respect to the whole column, and

(b) count the total number of observations in the column.

Then, for each column, I'm trying to do a mathematical expression, namely "rank/(count+1)", and assign that result to a variable. Here is my code so far for this sample data frame ("df").

    for (i in 1:ncol(df)) {
      assign(paste("x",i,sep=""),rank(-df[,i],ties.method="first"))
      assign(paste("y",i,sep=""),length(df[,i]))
      assign(paste("z",i,sep=""),(x[i]/(y[i]+1)))
    }

When I troubleshoot each "assign" line, the "x" and "y" variables work fine:

> x1
[1] 7 1 3 2 8 5 4 6

> x2
[1] 3 5 4 6 1 8 7 2

> x3
[1] 8 7 6 5 4 3 2 1

> x4
[1] 8 7 4 3 6 2 1 5

y1:4 (in this case) work fine, all resulting in a value of 8. The operation of x[i]/(y[i]+1), however, results in the following error:

Error in assign(paste("z", i, sep = ""), (x[i]/(y[i] + 1))) : 
  object 'y' not found

Anyone have any ideas? Once I calculate this new "z[i]" value, how do I create a new dataframe with those z values, so that I can move on to ggplot?

Upvotes: 0

Views: 1170

Answers (1)

Gavin Simpson
Gavin Simpson

Reputation: 174813

Is there a reason why this has to be done as a loop? Anyway, using your data

dat <- read.table(text = "    BR WT   SW   PO
1  4.0  7  2.0  1.0
2 12.0  5  4.0  2.0
3  8.0  7  5.0  8.6
4  9.0  3  5.6  9.0
5  1.0 10  6.0  5.0
6  6.0  2  7.5 12.0
7  7.0  3 10.0 15.0
8  4.5 10 12.0  6.0", header = TRUE)

this returns the ranks

r <- sapply(dat, function(x) rank(-x, ties.method = "first"))

> r
     BR WT SW PO
[1,]  7  3  8  8
[2,]  1  5  7  7
[3,]  3  4  6  4
[4,]  2  6  5  3
[5,]  8  1  4  6
[6,]  5  8  3  2
[7,]  4  7  2  1
[8,]  6  2  1  5

As length(dat[, i]) is the same for all i, and is given by nrow(dat), you can ignore the assignment of y and move straight to the last operation:

> r / (nrow(dat) + 1)
            BR        WT        SW        PO
[1,] 0.7777778 0.3333333 0.8888889 0.8888889
[2,] 0.1111111 0.5555556 0.7777778 0.7777778
[3,] 0.3333333 0.4444444 0.6666667 0.4444444
[4,] 0.2222222 0.6666667 0.5555556 0.3333333
[5,] 0.8888889 0.1111111 0.4444444 0.6666667
[6,] 0.5555556 0.8888889 0.3333333 0.2222222
[7,] 0.4444444 0.7777778 0.2222222 0.1111111
[8,] 0.6666667 0.2222222 0.1111111 0.5555556

assuming I understand what you are trying to do of course...

Upvotes: 2

Related Questions