umm - 2 months ago 4
R Question

# Recognising the "-" sign and separating Data

I want to change the result below into two rows : "Win" and "Loss". My aim is to separate the data ("3-2" to "3" and "2") but I do not know how to make R breaks them apart.

Can someone please enlighten me of any functions to separate them and put them in different variables ("Win" and "Loss")?

I have this Data:

``````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)
``````

### 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")
``````
Source (Stackoverflow)
Comments