Saturday, August 13, 2022

[FIXED] How to make a output result as a table form in R

Issue

I am having the three output results as below,

structure(c("[1] \"lls_loess\" \"lls_rlr\"   \"knn_loess\"", 
"[1] \"lls_loess\" \"lls_rlr\"   \"knn_loess\"", "[1] \"lls_loess\" \"lls_rlr\"   \"lls_vsn\"  "
), dim = c(1L, 3L), dimnames = list(NULL, c("Based_on_PCV", "Based_on_PEV", 
"Based_on_PMAD")))

I have used the names() code for extracting the names from the result output.

And I want to give the output to the user in the format as in the picture. example result format

Please suggest some R code for this matter.


Solution

Well... this is some quite ugly data, one possible solution

x=structure(c("[1] \"lls_loess\" \"lls_rlr\"   \"knn_loess\"", 
            "[1] \"lls_loess\" \"lls_rlr\"   \"knn_loess\"", "[1] \"lls_loess\" \"lls_rlr\"   \"lls_vsn\"  "
), dim = c(1L, 3L), dimnames = list(NULL, c("Based_on_PCV", "Based_on_PEV", 
                                            "Based_on_PMAD")))
df=data.frame(x)

data.frame(
  sapply(
    sapply(
      df,
      function(i){
        strsplit(
          gsub('"',",",gsub('\\[[0-9]+\\]| ',"",i)),
          ","
        )
      }
    ),
    function(j){j[j!=""]}
  )
)

  Based_on_PCV Based_on_PEV Based_on_PMAD
1    lls_loess    lls_loess     lls_loess
2      lls_rlr      lls_rlr       lls_rlr
3    knn_loess    knn_loess       lls_vsn


Answered By - user2974951
Answer Checked By - Katrina (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.