So I have this data :
data<-as.data.frame(matrix(1:9,ncol=3))
which gives this :
| V1 | V2 | V3 |
________________
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
I want to create a variable for each column to get this :
> V1
[1] 1 2 3
> V2
[1] 4 5 6
> V3
[1] 7 8 9
I know that if I do a loop with the assign function :
for (i in 1:length(data)) {
assign(names(data[i]),data[,i])
}
it works. But if I try with the "<-" :
for (i in 1:length(data)) {
names(data[i])<-data[,i]
}
it does not work. Why does it work with the assign function but not with the "<-" ?
question from:https://stackoverflow.com/questions/65876320/for-loop-with-to-create-variables-in-r