[R] Basic help on DF creation row by row

Rui Barradas ruipbarradas at sapo.pt
Tue Oct 1 20:38:16 CEST 2013


Hello,

The main problem is in the way you form newRow. You can't mix data 
classes in a vector created with c(), so all its elements become 
characters, the least common denominator:

newRow <- c("primitiveSpace", 1.1 , "mm2")
newRow
[1] "primitiveSpace" "1.1"            "mm2"


Then when you rbind it with the data frame, they are all converted to 
factors. This is because the default behavior is to have the option 
stringsAsFactors set to TRUE. Try to check it:

options()$stringsAsFactors  # TRUE


Now, each of those factors was created with only one level, so when you 
try to assign something different to them, with the second rbind, NAs 
are generated.

The correct way would be something like the following.


newRow <- data.frame(item = "primitiveSpace", value = 1.1 , unit = "mm2")
deviceSummary <- rbind(deviceSummary , newRow )
print(deviceSummary)
str(deviceSummary)  # to check what you have

newRow <- data.frame(item = "primitiveCellSpace", value = 2.2 , unit = 
"mm2")
deviceSummary <-rbind(deviceSummary , newRow )
print(deviceSummary )


Hope this helps,

Rui Barradas

Em 01-10-2013 14:24, rolf.kemper at renesas.com escreveu:
>
>
> Dear Experts
>
> I'm very new to R and after some days or reading and testing  I tried to make my first small application (and failed ...)
> In general I would like to work with sqldf , ggplot2 to create some graphical output.
>
> At present I got stuck with this:
>
> PROG #############################################################################
> deviceSummary <- data.frame(item = character(0) ,  value = numeric(0) , unit = character(0) )
> print ( sapply(deviceSummary, class))
>
> newRow <- c("primitiveSpace", 1.1 , "mm2")
> deviceSummary <-rbind(deviceSummary , newRow )
> print(deviceSummary)
>
> newRow <- c("primitiveCellSpace", 2.2 , "mm2")
> deviceSummary <-rbind(deviceSummary , newRow )
> print(deviceSummary )
>
> OUTPUT ############################################################################
>      item     value      unit
>   "factor" "numeric"  "factor"
>    X.primitiveSpace. X.1.1. X.mm2.
> 1    primitiveSpace    1.1    mm2
>    X.primitiveSpace. X.1.1. X.mm2.
> 1    primitiveSpace    1.1    mm2
> 2              <NA>   <NA>    mm2
> Warning messages:
> 1: In `[<-.factor`(`*tmp*`, ri, value = "primitiveCellSpace") :
>    invalid factor level, NA generated
> 2: In `[<-.factor`(`*tmp*`, ri, value = "2.2") :
>    invalid factor level, NA generated
>
> Inserting the first record went fine , but the next one will fail as you can see.
> Repeating only the first one (value1.1) went fine.
>
> May be my imagination of DF is totally wrong. Hope someone can guide me.
>
> Thanks a lot
>
> Rolf
>
>
>
>
> Rolf  Kemper, Manager, Mixed Signal Design, Networking, Renesas Electronics Europe GmbH, , Arcadiastr. 10, 40472, Duesseldorf, Germany,  Phone:+49 211
> 6503-1475, Fax:+49 211 6503-1540, mailto:Rolf.Kemper at renesas.com, http://www.renesas.eu
>
> This message is intended only for the use of the address...{{dropped:24}}
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list