[R] creating a data.frame from scratch

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Sat Apr 20 03:08:23 CEST 2019


You seem to be trying to learn R ... sideways... or backwards, perhaps.

Have you read An Introduction to R[1], included with every copy of the 
software? In particular, there are sections on data frames [2] (which 
should be read in the context of the discussion on lists, as it 
is presented. There is also the discussion of factors [3] where the idea 
of using integers to keep track of categorical data is discussed. There 
are many other introductory resources as well which would fill you in on 
these kinds of basic concepts if you find the ItR too computerish.

No R programmer I have ever met constructs data frames by typing in the 
kind of R you showed... that is distinctly characteristic of the 
output of the "dput" function, which is completely general and precise in 
an R-language sense and useful in reproducing whatever data you have in 
your R environment in someone elses environment.

So, one of these might be more typical:

dta1 <- data.frame( Fruit = c( "apple", "banana", "pear", "orange", "kiwi" )
                   , Color = c( "red", "yellow", "green", "orange", "green" )
                   , Shape = c( "round", "oblong", "pear", "round", "round" )
                   , Juice = Juice = c( 1, 0, 0.5, 1, 0 )
                   )

dta2 <- read.table( text =
"Fruit  Color  Shape  Juice
apple   red    round  1.0
banana  yellow oblong 0.0
pear    green  pear   0.5
orange  orange round  1.0
kiwi    green  round  0.0
", header = TRUE )

I would also strongly encourage you to read the Posting Guide mentioned at 
the bottom of every posting on this mailing list. One issue with your 
email is that sending HTML-formatted email to this list often leads to us 
receiving gibberish because this is a text-only mailing list and the 
translation from HTML to plain text is done differently by different mail 
handling software. Please find the setting for your email software that 
causes it to send plain text (Gmail has a button... you just have to look 
for it). Another issue is that there is an expectation on this list that 
you will have made some effort to wade through the documentation and at 
least mention what documentation you looked at so interested people can 
learn from your difficulty and possibly fix problems in the documentation 
for future users.

[1] https://cran.r-project.org/doc/manuals/r-release/R-intro.html
[2] https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Data-frames
[3] https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Factors

On Fri, 19 Apr 2019, Drake Gossi wrote:

> Hello everyone,
>
> Is there any way to create a data.frame from scratch? other than, say, this?
>
>> structure(list(Fruit = structure(c(1L, 2L, 5L, 4L, 3L), .Label =
> c("apple",
> "banana", "kiwi", "orange", "pear"), class = "factor"), Color =
> structure(c(3L,
> 4L, 1L, 2L, 1L), .Label = c("green", "orange", "red", "yellow"
> ), class = "factor"), Shape = structure(c(3L, 1L, 2L, 3L, 3L), .Label =
> c("oblong",
> "pear", "round"), class = "factor"), Juice = c(1, 0, 0.5, 1,
> 0)), class = "data.frame", row.names = c("1", "2", "3", "4",
> "5"))
>
>
> which yields
>
>   Fruit  Color  Shape  Juice
> 1  apple    red  round  1.0
> 2 banana yellow oblong   0.0
> 3   pear  green   pear   0.5
> 4 orange orange  round   1.0
> 5   kiwi  green  round   0.0
>
>
> I get *that* it works. I just don't know *how* it works, and whether or not
> there is another, easier way...
>
> For example,
>
>> structure(list(Fruit = structure(c(1L, 2L, 5L, 4L, 3L), .Label =
> c("apple", "banana", "kiwi", "orange", "pear") ...
>
>
> What on earth are these numbers? c(1L, 2L, 5L, 4L, 3L)? and why are they
> out of order?
>
> And then why put the fruits out of order? c("apple",
> "banana", "kiwi", "orange", "pear")? since that's not a descending order?
> since, in the final data.frame, it goes apple, banana, *pear*, *orange*,
> kiwi?
>
> So many questions!
>
> Drake
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil using dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k



More information about the R-help mailing list