Reputation: 171
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.
Upvotes: 0
Views: 1675
Reputation: 7292
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))
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
Upvotes: 1
Reputation: 76402
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
Upvotes: 1