0

I am trying to write a loop that takes variables in a column of one table (table x) and use those names as column names in another table (table y) and then populate those columns based on certain criteria. Here is my code. I have been attempting to use a For Loop.

for(player in x)
  {
  y$paste0(x$player)<-ifelse(y$Playing=="True", 1, 0)
  }

The error I am getting is "invalid function in complex assignment"

I am working with sports data. My end goal is to count the amount of passes each player was on the field for. I will need to assign the variable 1 for players while they are on the field and a 0 if they are on the bench. Any help would be greatly appreciated.

  • Please read [How to make a great reproducible example in R?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – M-- Aug 25 '17 at 16:35

2 Answers2

1

How about just:

#  create df1 with 10 random TRUE/FALSE values
df1 <- data.frame(Player= sample(c(TRUE,FALSE),10,TRUE))

df1$playing <- sapply(df1$Player, function(x) ifelse(x==TRUE,1,0))

Update:

require(tidyr)

df1 <- data.frame(ID = seq(1,35),Player= sample(c("TRUE","FALSE"),35,TRUE))
df1$playing <- sapply(df1$Player, function(x) ifelse(x==TRUE,1,0))
df1 <- df1[,-2]

spread(df1, ID, playing)

And generalized to multiple games (i.e. multiple rows for each player ID)

df1 <- data.frame(ID = rep(1:35,each=3),GameID=rep(1:3,35),Player= sample(c("TRUE","FALSE"),105,TRUE))
df1$playing <- sapply(df1$Player, function(x) ifelse(x==TRUE,1,0))
df1 <- df1[,-3]

spread(df1, ID, playing)

Sample output

  GameID   1 2 3 4
       1   0 1 0 0
       2   0 0 1 0
       3   0 1 0 0
Mako212
  • 6,787
  • 1
  • 18
  • 37
  • I am looking for a more generalized way than a c("TRUE","TRUE","FALSE") method because I have 35 rows and I will need to run this multiple times with varying data. – SoccerAnalytics26 Aug 25 '17 at 16:41
  • @SoccerAnalytics26 Can you explain more? I just picked "TRUE" "TRUE" "FALSE" to approximate what I assume your x$player column looks like. This code would work fine for any TRUE/FALSE vector – Mako212 Aug 25 '17 at 16:43
  • @SoccerAnalytics26 I just changed the way I built `df1` to make this more clear – Mako212 Aug 25 '17 at 16:47
  • I see. My player column assigns a player ID for each player. then there is a separate column for if that player started which is a T/F column. I am looking to add a column for each player (with their ID as the column name) and then populate the column with 0's and 1's based if they are on the field or on the bench. – SoccerAnalytics26 Aug 25 '17 at 16:56
  • @SoccerAnalytics26 see my update, I think that's what you're looking for – Mako212 Aug 25 '17 at 17:04
  • @SoccerAnalytics26 and if you have multiple rows per player ID (say one row for each game), you need to add a GameID column, and then using the same code you'll have player ID across the top as the column headers, each row will have a particular GameID – Mako212 Aug 25 '17 at 17:22
  • I think this helps a lot. Thank you. I'm still looking at it and need to change a few things to make it work exactly how I want but I think this helps. – SoccerAnalytics26 Aug 25 '17 at 17:26
  • @SoccerAnalytics26 No prob. Also be sure to change `lapply` to `sapply`. `lapply` returns a list, and that was screwing up the output so it wasn't really a correctly formated data frame. `sapply` fixes that. – Mako212 Aug 25 '17 at 17:34
1

You don't need paste0 in this case . For what I understand of your attempted code, something like the following might do the job.

First let's make up the data frames x and y.

set.seed(1)   # make it reproducible
x <- data.frame(A = 1:5, B = rnorm(5))
y <- data.frame(Playing = sample(c("True","False"), 10, TRUE))

Now, create the new columns.

for(player in names(x)) {
    y[[player]] <- ifelse(y$Playing == "True", 1, 0)
}

The columns were created.

str(y)
'data.frame':   10 obs. of  3 variables:
 $ Playing: Factor w/ 2 levels "False","True": 2 2 1 2 1 2 1 1 2 1
 $ A      : num  1 1 0 1 0 1 0 0 1 0
 $ B      : num  1 1 0 1 0 1 0 0 1 0
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • I think this is really close. Do you know how I could create a new column for each player in my y table since each player will have different 0's and 1's (for playing times). – SoccerAnalytics26 Aug 25 '17 at 16:49
  • @SoccerAnalytics26 The code above does create a new column for each column of `x`. I'll edit my code to make it more clear. – Rui Barradas Aug 25 '17 at 16:57