2

Ive tried many different things but I have no clue how to add a row to this table

  means <- data.frame("State" = character(0), "Mean" = numeric(0))

I thought it was something like this

for (state in unique(data$State)){
  means <- rbind(means, c("state", 4))
}

But when i try to print the table it gives me warnings about different levels.

44: In `[<-.factor`(`*tmp*`, ri, value = structure(c(1L, NA,  ... :
  invalid factor level, NA generated
45: In `[<-.factor`(`*tmp*`, ri, value = structure(c(1L, NA,  ... :
  invalid factor level, NA generated

EDIT:

print(state) prints this

[1] "Arizona"
[1] "California"
[1] "Colorado"
[1] "District Of Columbia"
[1] "Florida"
[1] "Illinois"
[1] "Indiana"
[1] "Kansas"
[1] "Kentucky"
[1] "Louisiana"
[1] "Michigan"
[1] "Missouri"
[1] "New Jersey"
[1] "New York"
[1] "North Carolina"
[1] "Oklahoma"
[1] "Pennsylvania"
[1] "Texas"
[1] "Virginia"
[1] "Massachusetts"
[1] "Nevada"
[1] "New Hampshire"
[1] "Tennessee"
[1] "South Carolina"
[1] "Connecticut"
[1] "Iowa"
[1] "Maine"
[1] "Maryland"
[1] "Wisconsin"
[1] "Country Of Mexico"
[1] "Arkansas"
[1] "Oregon"
[1] "Wyoming"
[1] "North Dakota"
[1] "Idaho"
[1] "Ohio"
[1] "Georgia"
[1] "Delaware"
[1] "Hawaii"
[1] "Minnesota"
[1] "New Mexico"
[1] "Rhode Island"
[1] "South Dakota"
[1] "Utah"
[1] "Alabama"
[1] "Washington"
[1] "Alaska"
smci
  • 32,567
  • 20
  • 113
  • 146
k9b
  • 1,457
  • 4
  • 24
  • 54
  • What is `data$State`? Is it a factor and if so, is `"state"` a level in `data$State`? – aichao Nov 15 '16 at 16:08
  • Made an edit to the original post – k9b Nov 15 '16 at 16:11
  • So, do you want to create a data frame with 50 rows (one for each state) where each row is `data.frame(State="state","Mean"=4)`? – aichao Nov 15 '16 at 16:15
  • Correct :) (I think there only about 47 rows in my data set, so if there is a way to create a table that I can append() to I rather do that) – k9b Nov 15 '16 at 16:20

2 Answers2

6

You are trying to add a vector and rbind it with data frame which is not the best option. You better rbind a data.frame to data.frame.

So in your case better to do:

for (state in unique(data$state)) {
    means<-rbind(means, data.frame(State=state,Mean=4)
}
Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51
1

You can write the code with the newer libraries dplyr, tidyr and purrr that provide more intuitive readability. The code is still very short:

map_df(states, function(state) { means %>% add_row(State = state, Mean = 4)})

Surprisingly (to me) - despite the overhead for dplyr - tidyr::add_row is about 23x faster than rbind and faster than many other methods:

df = data.frame(x = numeric(), y = character())

system.time(
  for (i in 1:100000) {
    df <- rbind(df, data.frame(x = i, y = toString(i)))
  }  
)
    user   system  elapsed 
1466.087  355.579 1827.724


system.time(
  map_df(1:100000, function(x) { df %>% add_row(x = x, y = toString(x)) })
)
   user  system elapsed 
 78.951   0.337  79.555
Agile Bean
  • 6,437
  • 1
  • 45
  • 53