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.

1

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

8 Comments

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 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
@SoccerAnalytics26 I just changed the way I built df1 to make this more clear
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 see my update, I think that's what you're looking for
|
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

2 Comments

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 The code above does create a new column for each column of x. I'll edit my code to make it more clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.