Rilcon42
Rilcon42

Reputation: 9763

printing results of equation to table

Edit:

I am trying to use a function on every combination of values in a vector and save the result to a n x n matrix. My description below is not nearly as concise. Thanks @Deleet

I am trying to print the results of a function to a table, rather then as a list. Can someone explain how to do this in general? I included my specific case below.

df<-data.frame(
    c1=c(1,2,3),
    c2=c(6,6,6),
    c3=c(23,44,66)
)

temp<-NULL
for(i in colnames(df)){
    for(j in colnames(df)){
        temp<-c(temp,paste0("function(i,j)_RESULT_HERE_FROM FUNCTION"))
    }
}

goal:

    c1           c2           c3
c1  RESULT_HERE  RESULT_HERE  RESULT_HERE
c2  RESULT_HERE  RESULT_HERE  RESULT_HERE
c3  RESULT_HERE  RESULT_HERE  RESULT_HERE

Upvotes: 1

Views: 56

Answers (1)

akrun
akrun

Reputation: 887691

You can try outer

temp <- outer(colnames(df), colnames(df), 
         FUN=function(x,y) paste0(y, "!!", x, "!!", "RESULT_HERE"))

Upvotes: 1

Related Questions