Dear friends. This is again a very simple question, and I hope you will accept it. I have a dataset with variables in columns all with their names. Now I want to add a column indicating simply identity of patients, numbered according to row. There are 34 rows. So I did:>ID <- c(1:34)but was not allowed to>Ptt <- cbind(patients,ID)and was informed that>Error: names attribute must be the same length as the vectorSo, I removed the names>newname <- names(patients) >names(patients) <- NULL >ptt <- cbind(patients,ID)but still got Error: names attribute must be the same length as the vector in spite of> names(ID)NULL> names(patients)NULL> attr(patients,"names")NULL> attr(ID,"names")NULL One thing, is that inconsistent ? Short of editing my data file, which would not be very difficult in this case, how could I do it ? Sincerely yours Troels Ring, MD -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>>>>> "Troels" == Troels Ring <tring at mail1.stofanet.dk> writes:Troels> This is again a very simple question, and I Troels> hope you will accept it. I have a dataset with variables Troels> in columns all with their names. Now I want to add a Troels> column indicating simply identity of patients, numbered Troels> according to row. There are 34 rows. So I did: >> ID <- c(1:34) Troels> but was not allowed to >> Ptt <- cbind(patients,ID)=20 Troels> and was informed that >> Error: names attribute must be the same length as the vector There may be a problem with the way R is handling your request but I think we would need to know a bit more about the form of the data before we could decide. Your initial description sounds as if the data are in a "data frame", the class of object created by the "read.table" function. One way to discover the form of the data is to use the "class" or, more generally, the "data.class" functions. For example, data I am using in a lecture today has this form R> T10.5 # Average SAT math scores by sex - Hocking, table 10.5 Year Mmath Fmath 1 67 514 467 2 68 512 470 3 69 513 470 4 70 509 465 5 71 507 466 6 72 505 461 7 73 502 460 8 74 501 459 9 75 495 449 10 76 497 446 11 77 497 445 12 78 494 444 R> data.class( T10.5 ) [1] "data.frame" If you find your data are in a matrix and you wish to convert them to a data frame, you can use the "as.data.frame" function. R> matSAT <- as.matrix( T10.5 ) R> matSAT Year Mmath Fmath 1 67 514 467 2 68 512 470 3 69 513 470 4 70 509 465 5 71 507 466 6 72 505 461 7 73 502 460 8 74 501 459 9 75 495 449 10 76 497 446 11 77 497 445 12 78 494 444 R> data.class( matSAT ) [1] "matrix" R> dfSAT <- as.data.frame( matSAT ) R> data.class( dfSAT ) [1] "data.frame" Generally data frames are preferred to matrices for storing data sets. If your data are in a data frame then you can create a new variable by assigning a component of that name in the data frame. It looks like R> dfSAT$ID <- 1:nrow(dfSAT) R> dfSAT Year Mmath Fmath ID 1 67 514 467 1 2 68 512 470 2 3 69 513 470 3 4 70 509 465 4 5 71 507 466 5 6 72 505 461 6 7 73 502 460 7 8 74 501 459 8 9 75 495 449 9 10 76 497 446 10 11 77 497 445 11 12 78 494 444 12 A more complete discussion is provided in section 2.1 of Venables and Ripley (1997), "Modern Applied Statistics with S-PLUS (second edition)". Please see also the "R Complements" file for the book as mentioned in http://www.stats.ox.ac.uk/pub/MASS2/Compl.shtml -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._