英文:
Copy and paste output from console to Excel
问题
我解释情感智商分数以维持生计,我的分析的一部分涉及查看分数之间的差异是否达到10分或更多。为了更轻松地执行这个操作,我在R中创建了以下函数:
eqi_function <- function(x) {
for(i in x) {
i <- abs(i - x[1:15])
print(i)
}
}
在这个函数中,x是一个包含15个分数的向量,例如:
test.scores <- c(112,122,122,98,101,106,106,116,100,123,123,122,122,115,115)
当我使用这个测试分数向量调用我的函数时,下面是输出:
[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
我想要复制并粘贴这个输出到Excel中,并使用条件格式设置来突出显示差异达到10分或更多的单元格。
我尝试过无数的函数(例如,write_clip、write.Table),我还尝试过将我的函数更改为以表格、矩阵和数据框的形式打印(i),但都没有生成这种输出。将它写入CSV文件对我来说也行不通,我不确定我做错了什么。
如果您认为有更好的方法来生成我寻找的结果,包括更改函数,请告诉我。我现在只使用R大约两个月!:-)
感谢您的建议、见解和专业知识。 - Tamara
英文:
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
答案1
得分: 1
你可以使用outer
更轻松地创建输出。而不是打印每一行,将整个内容存储在一个矩阵中:
output <- abs(outer(test.scores, test.scores, `-`))
output
要使其适合粘贴到Excel中,你可以执行以下操作:
writeClipboard(apply(output, 1, paste, collapse = "\t"))
现在,到电子表格中,只需选择一个单元格,然后粘贴即可。
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论