1

I interpret emotional intelligence scores for a living, and part of my analysis involves looking at differences of 10 or more points between scores. To do this more easily, I created the following function in r:

eqi_function <- function(x) {
  for(i in x) {
    i <- abs(i - x[1:15])
      print(i) 
    }
}

In this function, x is a vector of 15 scores, e.g.:

test.scores <- c(112,122,122,98,101,106,106,116,100,123,123,122,122,115,115)

When I call my function using this vector of test scores, here's the output:

 [1]  0 10 10 14 11  6  6  4 12 11 11 10 10  3  3
 [1] 10  0  0 24 21 16 16  6 22  1  1  0  0  7  7
 [1] 10  0  0 24 21 16 16  6 22  1  1  0  0  7  7
 [1] 14 24 24  0  3  8  8 18  2 25 25 24 24 17 17
 [1] 11 21 21  3  0  5  5 15  1 22 22 21 21 14 14
 [1]  6 16 16  8  5  0  0 10  6 17 17 16 16  9  9
 [1]  6 16 16  8  5  0  0 10  6 17 17 16 16  9  9
 [1]  4  6  6 18 15 10 10  0 16  7  7  6  6  1  1
 [1] 12 22 22  2  1  6  6 16  0 23 23 22 22 15 15
 [1] 11  1  1 25 22 17 17  7 23  0  0  1  1  8  8
 [1] 11  1  1 25 22 17 17  7 23  0  0  1  1  8  8
 [1] 10  0  0 24 21 16 16  6 22  1  1  0  0  7  7
 [1] 10  0  0 24 21 16 16  6 22  1  1  0  0  7  7
 [1]  3  7  7 17 14  9  9  1 15  8  8  7  7  0  0
 [1]  3  7  7 17 14  9  9  1 15  8  8  7  7  0  0

I would like to copy and paste this output into Excel and use conditional formatting to highlight cells with a difference of 10 or more points.

I've tried countless functions (e.g., write_clip, write. Table) and I've also tried changing my function to print (i) as a table, as a matrix, and as a dataframe, which doesn't create this output. Writing it into a csv. isn't working for me either and I'm not sure what I'm doing wrong.

If you think there's a better method for producing the results I am looking for, including changing the function, let me know. I've only been using r for about 2 months now! :-)

Appreciate your suggestions, insight, and expertise. - Tamara

Afshin
  • 103
  • 6
  • Have you tried other solutions in stackoverflow? for example, https://stackoverflow.com/questions/7096989/how-to-save-all-console-output-to-file-in-r – Afshin Feb 09 '23 at 22:35
  • Thanks, Afshin. I've been checking many solutions in StackOverflow. I appreciate the link but my issue is a little nuanced and the output I'm trying to transfer to excel is different from the user here. – Tamara Kellam-Washington Feb 09 '23 at 23:27

1 Answers1

1

You can certainly create your output more easily using outer. Rather than printing each row, store the whole thing in a matrix:

output <- abs(outer(test.scores, test.scores, `-`))

output
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
#>  [1,]    0   10   10   14   11    6    6    4   12    11    11    10    10     3     3
#>  [2,]   10    0    0   24   21   16   16    6   22     1     1     0     0     7     7
#>  [3,]   10    0    0   24   21   16   16    6   22     1     1     0     0     7     7
#>  [4,]   14   24   24    0    3    8    8   18    2    25    25    24    24    17    17
#>  [5,]   11   21   21    3    0    5    5   15    1    22    22    21    21    14    14
#>  [6,]    6   16   16    8    5    0    0   10    6    17    17    16    16     9     9
#>  [7,]    6   16   16    8    5    0    0   10    6    17    17    16    16     9     9
#>  [8,]    4    6    6   18   15   10   10    0   16     7     7     6     6     1     1
#>  [9,]   12   22   22    2    1    6    6   16    0    23    23    22    22    15    15
#> [10,]   11    1    1   25   22   17   17    7   23     0     0     1     1     8     8
#> [11,]   11    1    1   25   22   17   17    7   23     0     0     1     1     8     8
#> [12,]   10    0    0   24   21   16   16    6   22     1     1     0     0     7     7
#> [13,]   10    0    0   24   21   16   16    6   22     1     1     0     0     7     7
#> [14,]    3    7    7   17   14    9    9    1   15     8     8     7     7     0     0
#> [15,]    3    7    7   17   14    9    9    1   15     8     8     7     7     0     0

To make it suitable for pasting into Excel, you could do:

writeClipboard(apply(output, 1, paste, collapse = "\t"))

Now, over in the spreadsheet, just select a cell and paste:

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Hi Allen, thanks so much for your suggestions. I've updated my function to include the abs(outer(x, x, "-") code, which produced a matrix; however, when running the "writeClipboard" function I get the following error message: "the dim(X) must have a positive length." Any idea what's happening here? – Tamara Kellam-Washington Feb 09 '23 at 23:24
  • @TamaraKellam-Washington if you use the `test.scores` object from your example, and copy-paste my code exactly, you won't get this error. My guess is that you are mixing up a variable name somewhere. Did you remember to store the result of `outer` as `output` perhaps? – Allan Cameron Feb 09 '23 at 23:27
  • Thanks so much. I realized I needed to update my function again and remove the "for (i in x)" since the outer function is enough. Removing the "for loop" from the function fixed the issue (and saving it as a function because I will be using it over and over again). – Tamara Kellam-Washington Feb 09 '23 at 23:33