[R] creating horizontal dataframes with column names

(Ted Harding) Ted.Harding at manchester.ac.uk
Wed Sep 17 13:01:27 CEST 2008


On 17-Sep-08 05:27:40, Alexy Khrabrov wrote:
> Exactly -- also found creating horizontal vector helps:
> 
>  > df <- data.frame(matrix(1:5,nrow=1))
>  > colnames(df) <- LETTERS[1:5]
>  > df
>    A B C D E
> 1 1 2 3 4 5
> 
> Thanks,
> Alexy

Another (though similar) approach (e.g. if you want to build up
the dataframe by successive rows):

  column.names <- LETTERS[1:3]
  values <- 1:3
  M<-rbind(NULL,values); colnames(M)<-column.names; rownames(M)<-NULL
  M
#      A B C
# [1,] 1 2 3
  values <- c(4,5,6)
  M<-rbind(M,values);  rownames(M)<-NULL

  M.df <- data.frame(M)
  M.df
#   A B C
# 1 1 2 3
# 2 4 5 6

Ted.

> On Sep 17, 2008, at 1:17 AM, Moshe Olshansky wrote:
> 
>> If df is your data.frame, then
>> colnames(df) <- c("col1","Col2","COL3")
>>
>>
>>
>> --- On Wed, 17/9/08, Alexy Khrabrov <deliverable at gmail.com> wrote:
>>
>>> From: Alexy Khrabrov <deliverable at gmail.com>
>>> Subject: [R] creating horizontal dataframes with column names
>>> To: r-help at stat.math.ethz.ch
>>> Received: Wednesday, 17 September, 2008, 1:52 PM
>>> Greetings -- in order to write back to SQL databases, one
>>> needs to
>>> create a dataframe with values.  I can get column names of
>>> an existing
>>> table with sqlColumns.  Say I have a vector of values (if
>>> they're all
>>> the same type), or a list (if different).  How do I create
>>> a dataframe
>>> with column names given by my sqlColumns?  To make it
>>> concrete, how do
>>> we make a dataframe
>>>
>>> A B C
>>> 1 2 3
>>>
>>> out of
>>>
>>> column.names <- LETTERS[1:3]
>>> values <- 1:3
>>>
>>> ?
>>> Cheers,
>>> Alexy
>>>
>>> ______________________________________________
>>> 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.
> 
> ______________________________________________
> 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.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 17-Sep-08                                       Time: 12:01:24
------------------------------ XFMail ------------------------------



More information about the R-help mailing list