umm umm - 3 months ago 14
R Question

Recognising the "-" sign and separating Data into new rows

I want to change the result below into two rows : "Win" and "Loss". My aim is to separate the data and make a simple line plot for "Win" and "Loss" later. Yes, I can just manually make new variables and input them myself given the small amount of data. However, I want to learn how to take care of this sort of data for future use, as you can see, they are glued together with the "-" (negative) sign and I have no idea how to separate them.

Can someone please enlighten me of any functions to separate them and put them in different rows?

First, I'm hoping R can recognise the separator "-", and then put the numbers in new rows called "Win" and "Loss" respectively.

d <- matrix(1:2, ncol = 6)
colnames(d) <- c("" , "2005" , "2006" ,"2007", "W-L" , "Win %")
d[1,1] <- "Win-Loss"
d[1,2] <- "3-2"
d[1.3] <- "6-4"
d[1,4] <- "5-2"
d[1,5] <- "14-8"
d[1,6] <- "63.63"

Answer

Here is one option to get the line plot

m1 <- t(sapply(tennis4[grep("^[0-9]+$", names(tennis4))], 
         function(x) scan(text=x, what = numeric(), quiet=TRUE, sep="-")))
colnames(m1) <- scan(text=tennis4[,1], what = "", quiet=TRUE, sep="-")
matplot(m1, type = "l", xaxt = "n", col = c(2, 4), ylab = "value")
axis(1, at = seq_len(nrow(m1)), labels = row.names(m1))
legend("topright", inset=.05, legend= colnames(m1), pch=1, col=c(2,4), horiz=TRUE)

enter image description here

data

tennis4 <- structure(list(Tournament = "Win-Loss", `2005` = "3-2", `2006` = "6-4", 
`2007` = "5-2", `2008` = "12-4", `2009` = "15-4", `2010` = "16-4", 
`2011` = "21-4", `2012` = "22-3", `2013` = "17-2", `2014` = "17-4", 
`2015` = "19-4", `2016` = "19-2", `W-L` = "172-39", `Win %` = 81.52), 
.Names = c("Tournament", 
"2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", 
"2013", "2014", "2015", "2016", "W-L", "Win %"), row.names = c(NA, 
-1L), class = "data.frame")