1

Here is my question: How can I use the tidyverse or base-r functions on this so I get

Judge poster score

from

Judge poster score poster score poster score ...

library(tidyverse)
pn <- paste0("poster_", as.character(1:10))
sn <- paste0("score_", as.character(1:10))
cn <- as.vector(rbind(pn, sn))
df <- read_delim(file = "
j1 4 98 5 95 6 89
j2 1 88 2 79 3 90
j3 1 78 4 88 6 87
j4 2 77 3 87 5 92
j5 4 98 5 95 6 89
j6 1 88 2 79 3 90
j7 1 78 4 88 6 87
j8 2 77 3 87 5 92
j9 1 88 2 88 3 87 4 90 5 91 6 90
j10 1 66 2 56                 
",
                delim = " ",
                trim_ws = TRUE,
                col_names = c("judge", cn))

I have a data.table solution:

library(data.table)
x <-melt(setDT(as.data.frame(df)),
     id.vars = "judge",
     measure = patterns("^poster", "^score"),
     verbose = TRUE)
x <- x[complete.cases(x)][, c(1, 3, 4)]
names(x) <- c("judge", "poster", "score")
  • As mentioned in the duplicate link `gather(df, key, val, -judge, na.rm = TRUE) %>% separate(key, into = c('key1', 'key2')) %>% spread(key1, val, fill = 0) %>% select(-key2)` – akrun Mar 08 '18 at 16:47

0 Answers0