识别“ - ”符号并分离数据

时间:2016-08-22 08:28:02

标签: r data-manipulation

我是R的完全初学者。我的数据由@akrun生成,以下是代码:

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")
  1. 我想将“Win-Loss”的分为“Win”和“Loss”。我的目标是将数据(“3-2”分别为“3”和“2”)分开并将它们存储在各自的位置,但我不知道如何让R将它们分开。有人可以告诉我任何将它们分开的功能吗?

  2. 将它们分开后,我可以使用哪个功能在同一张桌子上绘制2个不同的变量进行比较?

1 个答案:

答案 0 :(得分:0)

以下是获取折线图的一个选项

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

数据

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