0

I have read similar headings and unfortunately couldnt find an answer. The problem is that i have the following data:

 [,1] [,2] [,3] [,4] [,5] HomeTeam "Arsenal" "Leicester" "Man United" "QPR" "Stoke" AwayTeam "Crystal Palace" "Everton" "Swansea" "Hull" "Aston Villa" 

and i want it to be as follows:

 Teams Opponent HomeTeam Arsenal Crystal Palace AwayTeam Crystal Palace Arsenal HomeTeam1 Leicester Everton AwayTeam1 Everton Leicester HomeTeam2 Man United Swansea AwayTeam2 Swansea Man United 

i have tried the following command but its incorrect, just to give you an idea what i am trying to do:

odd <- seq(1, 759, by = 2) even <- seq(2, 270, by = 2) df <- data.frame(team = df_1[odd,1], opponent = df_1[even,1]) 

Question is: how can i modify the command above ? Or can u suggest me any other code. Thanks in advance!

4
  • See command t. Also ??transpose would have given you the answer to "how to transpose data?". Commented Apr 11, 2016 at 15:58
  • @VincentGuillemot The format requested by the OP is not a simple transposition. There are a few more operations to be considered to obtain the desired output. Commented Apr 11, 2016 at 16:16
  • @RHertel: you are absolutely right, that is why I wrote it as a comment and not an answer. ;) Commented Apr 11, 2016 at 16:35
  • 1
    I would also like to ask the OP if such a complicated transformation of the original data is really necessary: the final result looks really messy to me. (or should I say Messi?) Commented Apr 11, 2016 at 16:37

1 Answer 1

1

Say you have this:

m <- structure(c("Arsenal", "Crystal Palace", "Leicester", "Everton", "Man United", "Swansea", "QPR", "Hull", "Stoke", "Aston Villa" ), .Dim = c(2L, 5L), .Dimnames = list(c("HomeTeam", "AwayTeam" ), NULL)) 

You can obtain your result with:

res<-cbind.data.frame(Teams=c(m),Opponent=c(m[2:1,])) rownames(res)<-make.unique(rep(c("HomeTeam","AwayTeam"),ncol(m)),sep="") # Teams Opponent #HomeTeam Arsenal Crystal Palace #AwayTeam Crystal Palace Arsenal #HomeTeam1 Leicester Everton #AwayTeam1 Everton Leicester #HomeTeam2 Man United Swansea #AwayTeam2 Swansea Man United #.... 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.