I have a dataframe of which a part looks as follows:
library(data.table)
df <- fread(
"round1 round2 round3 round4 round5 round6 a
8 12 18 32 40 36 32
6 12 18 24 30 36 30
8 16 18 24 30 36 18
4 12 12 24 30 36 24
6 16 24 32 40 48 24
8 12 18 24 30 36 30
8 12 18 24 30 36 18
8 16 24 32 40 48 40
8 16 24 24 30 48 48",
header = TRUE
)
I would like to make column called x, which simply selects the round equal to the entry in a. I tried to following, but the syntax is somehow not right.
for (i in df$a){
if (df$a[i] = df$round2[i]){
df$x[i] = 1
} ifelse (df$a[i] = df$round3[i]){
df$x[i] = 2
} ifelse (df$a[i] = df$round4[i]){
df$x[i] = 3
} ifelse (df$a[i] = df$round5[i]){
df$x[i] = 4
} ifelse (df$a[i] = df$round6[i]){
df$x[i] = 5
} ifelse (df$a[i] = df$round7[i]){
df$x[i] = 6
}
}
Desired output:
library(data.table)
df2 <- fread(
"round2 round3 round4 round5 round6 round7 a x
8 12 18 32 40 36 32 4
6 12 18 24 30 36 30 5
8 16 18 24 30 36 18 3
4 12 12 24 30 36 24 4
6 16 24 32 40 48 24 3
8 12 18 24 30 36 30 5
8 12 18 24 30 36 18 3
8 16 24 32 40 48 40 5
8 16 24 24 30 48 48 6",
header = TRUE
)
Aucun commentaire:
Enregistrer un commentaire