-2

I'm having trouble relocating rows from my .csv table. Here's an example:

Let's say I have an 8x2 table and I want to turn it into a 4x3 table, using the matchid as a reference. Example:

matchid | playerid
      1 |     23
      1 |     15
      1 |     12
      1 |     65
      1 |     21
      1 |     45
      1 |     123
      1 |     11

Expected:

matchid | playerid.x | playerid.y
      1 |     23     |     21
      1 |     15     |     45
      1 |     12     |     123
      1 |     65     |     11

I've tried using a function to traverse my table and fill in a new array:

collectData = function (data){
  matriz = matrix() 
  count = 1
  col = 1
  for(i in 1:nrow(data)){
    if(count < 4){
      matriz[i,col] = data[i,2]
      count = count + 1
    } 
    if (count == 4) {
      count = 0
      col = col + 1
    }
  }
  return(matriz)
}

Edit :

The CSV table has 3 columns and n rows, I want to split it by rows in half then put the second half aside from the first half

3
  • 3
    @Danilo Languages other than English are not allowed in SO (there's a Portuguese version pt.stackoverflow.com), this time I translated it the next time other members will close your post
    – user10434643
    Apr 10, 2019 at 21:21
  • 3
    @DaniloSilva However, you question is not quite clear, that the CSV have a static size (8x2) and you want to split it to 2 equal groups (x and y) or is there another criteria to identify x's values from y's ? (When you say "using the matchid as a reference" that would mean that its value is what determine whether to assign playerid to x or y but your example says otherwise since all matchid values equal to 1)
    – user10434643
    Apr 10, 2019 at 21:29
  • @BenChaliahAyoub The table is bigger than that, this is just a piece of it. It has only 2 columns, the first column (macthid) goes from 1 to n. The second column (playerid) has the id of the relevant players that match. The x and y is to demonstrate that I split in two teams for the same match. Apr 10, 2019 at 21:33

1 Answer 1

0

Based on what you stated in your comment, I suggest this simple approach.

If CSV isa Dataframe (Otherwise you can convert it), Here's an example:

In[1]: df = data.frame(cbind(c(1,1,1,1), c(1,2,3,4)))
Out[2]:
  X1 X2
1  1  1
2  1  2
3  1  3
4  1  4

In[3]: x <- subset(df, as.numeric(rownames(df))<=(length(df$X2)/2))
In[4]: y <- subset(df, as.numeric(rownames(df))>(length(df$X2)/2))
In[5]: z <- data.frame(cbind(matchid=df$X1, playerid.x=x[,2], playerid.y=y[,2]))
In[6]: head(z,(length(z[,1])/2))
Out[7]:
     matchid playerid.x playerid.y
[1,]       1          1          3
[2,]       1          2          4
2
  • In case your dataframe still has the duplicate data. Would you rather remove them instead of calling the head? Because there may be a matchid = 2 with other playerid. Apr 10, 2019 at 22:12
  • You can just store the head return in z and they'll be removed.
    – user10434643
    Apr 10, 2019 at 22:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.