-1

I've been fighting with these problem for a while, but is there a way to get this in R?

Source Table 
╔═══════════╦══════╦═══════╗
║ TEST_NAME ║ SBNO ║  VAL  ║
╠═══════════╬══════╬═══════╣
║ Test1     ║    1 ║ 0.304 ║
║ Test1     ║    2 ║ 0.31  ║
║ Test1     ║    3 ║ 0.306 ║
║ Test2     ║    1 ║ 2.3   ║
║ Test2     ║    2 ║ 2.5   ║
║ Test2     ║    3 ║ 2.4   ║
║ Test3     ║    1 ║ PASS  ║
║ Test3     ║    2 ║ PASS  ║
╚═══════════╩══════╩═══════╝


Desired Output 
╔══════════════════════════╗
║ SBNO Test1 Test2   Test3 ║
╠══════════════════════════╣
║ 1    0.304  2.3    PASS  ║
║ 2    0.31   2.5    PASS  ║
║ 3    0.306  2.4    NULL  ║
╚══════════════════════════╝

*This example is not subsetted.

Thank you very much, Rodrigo Guinea

Rodrigo Guinea
  • 328
  • 4
  • 16
  • 2
    Can you please edit the question with the output of `dput(SourceTable)`? I realise you had a great deal of trouble formating the tables with the frames but it's **more difficult** for us to recreate them in an R session. – Rui Barradas Aug 08 '18 at 19:51

2 Answers2

1

Consider your data.frame is df, We can use spread

> library(tidyr)
> df %>% spread(TEST_NAME, VAL)
  SBNO Test1 Test2 Test3
1    1 0.304   2.3  PASS
2    2  0.31   2.5  PASS
3    3 0.306   2.4  <NA>
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
0

Looks like @JilberUrbina beat me to it, but here's a working example of essentially the same thing:

df <- data.frame(
    name = paste0("test", c(1,1,1,2,2,2,3,3)),
    sbno = rep(1:3, 3)[1:8],
    val  = c(0.304, 0.31, 0.306, 2.3, 2.5, 2.4, "pass", "pass"),
    stringsAsFactors = FALSE
)

tidyr::spread(df, key="name", value="val", convert=TRUE)
DanY
  • 5,920
  • 1
  • 13
  • 33