mercredi 7 mars 2007

Progressing

i tried to add a new column to my data set for my project but i can't find the command to do it, because i created a new vector that i would like to incorporate to my dataset. there is the same number of values as in my dataset, but the problem is that there is the same number if the name of the vector is included as a new header for the new column. is there a way to do that? and on top of that, i would like this new column to be added in my csv file too, so not doing it by using the attach() command. is it possible?

Mike :
try "cbind()" and "write.csv()" as follows:
> attach(faithful)
> head(faithful)
eruptions waiting
1 3.600 79
2 1.800 54
3 3.333 74
4 2.283 62
5 4.533 85
6 2.883 55
> length(eruptions)
[1] 272
> new.data.vector = rnorm(272, mean=100, sd=25)
> head(new.data.vector)
[1] 121.88050 78.59724 99.48620 98.39534 88.22590 82.78630
> new.data.frame = cbind(faithful, new.data.vector)
> head(new.data.frame)
eruptions waiting new.data.vector
1 3.600 79 121.88050
2 1.800 54 78.59724
3 3.333 74 99.48620
4 2.283 62 98.39534
5 4.533 85 88.22590
6 2.883 55 82.78630
> write.csv(new.data.frame, file="new_file.csv")

Aucun commentaire: