-4

I am a complete beginner in R. My data below was generated by @akrun and here is the code:

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. I want to separate the values of "Win-Loss" into "Win" and "Loss". My aim is to separate the data ("3-2" to "3" and "2") and store them in their respective places, but I do not know how to make R breaks them apart. Can someone please enlighten me of any functions to separate them?

  2. After separating them apart, which function can I use to plot 2 different variables on the same table for comparison?

umm
  • 61
  • 8
  • 1
    `tidyr::gather`, `tidyr::separate`, but please don't post pictures of data; read [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – alistaire Aug 22 '16 at 08:40
  • What is your expected output? – akrun Aug 22 '16 at 08:56
  • Sorry about that. I have edited the post so it doesn't contain pictures of data. Hi akrun, I want to put the numbers (3,6,5) into a new row called "Win" and (2,4,2) into "Loss". – umm Aug 22 '16 at 09:32

1 Answers1

0

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")
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Hi akrun, thank you for your answer. Yes it works. I'm a total beginner in R. Do you mind telling me where you get the "^[0-9]+$" to tell R of the negative sign? – umm Aug 22 '16 at 09:58
  • @umm It is for subsetting the columns that have names with only numbers i.e. 2005:2016. It says that from the start of the string `^`, match one or more numbers (`[0-9]+`) until the end `$` of the string. – akrun Aug 22 '16 at 09:59
  • thank you for your explanation! one more thing, when you re-create my data, what is the meaning of "row.names = c(NA,-1L)" ? It seems that without this, the data falls apart. – umm Aug 22 '16 at 12:45
  • @umm It is the output of `dput` i.e. after you read your data with `read.csv/read.table`. `tennis4 <- read.table("yourfile.txt", header=TRUE, stringsAsFactors=FALSE, check.names = FALSE); dput(head(tennis4))` gives that output. – akrun Aug 22 '16 at 14:18
  • So actually you just used dput(head(tennis4)) to convert the table I scraped into R code? that's pretty fancy. Does it work with any tables? – umm Aug 23 '16 at 11:40
  • @umm It is just to make it reproducible so that others can copy and paste in their console and get the exact output. If you didn't do it, we don't know whether you have a character class/factor class etc. This gives the exact structure of my dataset – akrun Aug 23 '16 at 11:42
  • 1
    Thank you akrun. That will make my questions more understandable and solvable. Really appreciate your answers – umm Aug 23 '16 at 11:44