如何将行添加到数据框?
How to add row to dataframe?
我尝试了很多不同的东西,但我不知道如何向其中添加一行 table
means <- data.frame("State" = character(0), "Mean" = numeric(0))
我以为是这样的
for (state in unique(data$State)){
means <- rbind(means, c("state", 4))
}
但是当我尝试打印 table 时,它会给我关于不同级别的警告。
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
编辑:
print(state) 打印这个
[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"
您正在尝试添加一个矢量,并且 rbind
它带有数据框,这不是最佳选择。你最好 rbind
一个 data.frame
到 data.frame
。
所以在你的情况下最好这样做:
for (state in unique(data$state)) {
means<-rbind(means, data.frame(State=state,Mean=4)
}
您可以使用更新的库 dplyr、tidyr 和 purrr 编写代码,这些库提供更直观的可读性。代码还是很短的:
map_df(states, function(state) { means %>% add_row(State = state, Mean = 4)})
令人惊讶的是(对我来说)——尽管 dplyr 的开销——tidyr::add_row 比 rbind 快 23 倍,比许多 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
我尝试了很多不同的东西,但我不知道如何向其中添加一行 table
means <- data.frame("State" = character(0), "Mean" = numeric(0))
我以为是这样的
for (state in unique(data$State)){
means <- rbind(means, c("state", 4))
}
但是当我尝试打印 table 时,它会给我关于不同级别的警告。
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
编辑:
print(state) 打印这个
[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"
您正在尝试添加一个矢量,并且 rbind
它带有数据框,这不是最佳选择。你最好 rbind
一个 data.frame
到 data.frame
。
所以在你的情况下最好这样做:
for (state in unique(data$state)) {
means<-rbind(means, data.frame(State=state,Mean=4)
}
您可以使用更新的库 dplyr、tidyr 和 purrr 编写代码,这些库提供更直观的可读性。代码还是很短的:
map_df(states, function(state) { means %>% add_row(State = state, Mean = 4)})
令人惊讶的是(对我来说)——尽管 dplyr 的开销——tidyr::add_row 比 rbind 快 23 倍,比许多 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