Mary A. Marion
2009-Jul-09 17:50 UTC
[R] naming of columns in R dataframe consisting of mixed data (alphanumeric and numeric)
Hello, I have an r function that creates the following dataframe tresults2. Notice that column 1 does not have a column heading. Tresults2: [,1] estparam 18.00000 nullval 20.00000 . . . ciWidth 2.04622 HalfInterval 1.02311 pertinent code: results<-cbind( estparam, nullval, t, pv_left, pv_right, pv_two_t, estse, df, cc, tbox, llim, ulim, ciWidth, HalfInterval) tresults<-round((results),5) tresults2<-data.frame(t(tresults)) names(tresults2)<-c("Statistic ", "Value") tresults2 ==========================================================================================After transposing my dataframe tresults2 consists of 2 columns. Col1=alphanumeric data (this really is a variable name) and col2=numeric data (this is value of varaiable). how do I name columns when columns are different (alphanumeric and numeric) to avoid the following error: Error in names(tresults2) <- c("Statistic ", "Value") : 'names' attribute [2] must be the same length as the vector [1] Am I to use c( , ) or list( , ) with a dataframe? Thank you for your help. Sincerely, Mary A. Marion [[alternative HTML version deleted]]
Etienne B. Racine
2009-Jul-09 19:49 UTC
[R] naming of columns in R dataframe consisting of mixed data (alphanumeric and numeric)
Mary A. Marion-2 wrote:> > Hello, > > I have an r function that creates the following dataframe tresults2. > Notice that column 1 does not have a column heading. > > Tresults2: > [,1] > estparam 18.00000 > nullval 20.00000 > . . . > ciWidth 2.04622 > HalfInterval 1.02311 > > pertinent code: > results<-cbind( estparam, nullval, t, pv_left, pv_right, pv_two_t, > estse, df, cc, tbox, llim, ulim, ciWidth, HalfInterval) > tresults<-round((results),5) > tresults2<-data.frame(t(tresults)) > names(tresults2)<-c("Statistic ", "Value") > tresults2 > > ==========================================================================================> After transposing my dataframe tresults2 consists of 2 columns. > Col1=alphanumeric data (this really is a variable name) and > col2=numeric data (this is value of varaiable). > > how do I name columns when columns are different (alphanumeric and > numeric) > to avoid the following error: > > Error in names(tresults2) <- c("Statistic ", "Value") : > 'names' attribute [2] must be the same length as the vector [1] >You tried to give a name to a column that do not exist (the second one you called "value"). You can check that using str(tresults2). The name of the columns should not (well, I don't see how) interfere with the type (or class) they contain. You can create a Value column by using tresults2$Value <- your_value> > Am I to use c( , ) or list( , ) with a dataframe? >You picked the right one ( c() )... to give the names, I guess this is what you were asking. Cheers, Etienne> Thank you for your help. > Sincerely, > Mary A. Marion >-- View this message in context: http://www.nabble.com/naming-of-columns-in-R-dataframe-consisting-of-mixed-data-%28alphanumeric-and-numeric%29-tp24411614p24416676.html Sent from the R help mailing list archive at Nabble.com.
Moshe Olshansky
2009-Jul-10 04:52 UTC
[R] naming of columns in R dataframe consisting of mixed data (alphanumeric and numeric)
Hi Mary, Your data.frame has just one column (not 2)! You can check this by dim(tresult2). What appears to you to be the first column (names) are indeed rownames. If you really want to have two columns do something like tresult2 <- cbind(colnames(tresult),data.frame(t(tresult),row.names=NULL)) Now tresult2 contains 2 columns and you can proceed with names(tresults2)<-c("Statistic ", "Value") --- On Fri, 10/7/09, Mary A. Marion <mmstat at comcast.net> wrote:> From: Mary A. Marion <mmstat at comcast.net> > Subject: [R] naming of columns in R dataframe consisting of mixed data (alphanumeric and numeric) > To: r-help at r-project.org > Received: Friday, 10 July, 2009, 3:50 AM > Hello, > > I have an r function that creates the following dataframe > tresults2. > Notice that column 1 does not have a column heading. > > Tresults2: > ? ? ? ? ? ? ? > ???[,1] > estparam? ???18.00000 > nullval? ? ? 20.00000 > . . . > ciWidth? ? ???2.04622 > HalfInterval? 1.02311 > > pertinent code: > results<-cbind( estparam, nullval, t, pv_left, pv_right, > pv_two_t, > estse, df,? cc, tbox, llim, ulim, ciWidth, > HalfInterval) > tresults<-round((results),5) > tresults2<-data.frame(t(tresults)) > names(tresults2)<-c("Statistic ", "Value") > tresults2 > > ==========================================================================================> After transposing my dataframe tresults2 consists of 2 > columns.? > Col1=alphanumeric data (this really is a variable name) > and > col2=numeric data (this is value of varaiable). > > how do I name columns when columns are different > (alphanumeric and numeric) > to avoid the following error: > > Error in names(tresults2) <- c("Statistic ", "Value") : > ? 'names' attribute [2] must be the same length as the > vector [1] > > Am I to use c( , ) or list( , ) with a dataframe? > > Thank you for your help. > Sincerely, > Mary A. Marion > > > > > > ??? [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org > mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code. >
Reasonably Related Threads
- Nonlinear statistical modeling -- a comparison of R and AD Model Builder
- Calculating the t-test for each row
- Bootstrap Question
- scan a directory and then make a string vector consisting of file names
- How to obtain a sequence of dates consisting of only weekdays