r - 选择数据框中的下一行以便在 R 中插入数据

标签 r dataframe data-entry

我有一个数据框,例如:

   name    age
1 "Danny"  20
2 "Mitt"   35
3 "Dylan"  8

当我得到新条目时,我想更新这个 df。 我已将 nrow(df) + 1 用于下一行:

df[nrow(df) + 1, ] <- c("Tom", 4)

还有其他方法吗?

最佳答案

你可以使用 rbind:

rbind(df,list("Tom",4))

检查 ?rbind:

The functions cbind and rbind are S3 generic, with methods for data frames. The data frame method will be used if at least one argument is a data frame and the rest are vectors or matrices. There can be other methods; in particular, there is one for time series objects. See the section on ‘Dispatch’ for how the method to be used is selected. If some of the arguments are of an S4 class, i.e., isS4(.) is true, S4 methods are sought also, and the hidden cbind / rbind functions from package methods maybe called, which in turn build on cbind2 or rbind2, respectively. In that case, deparse.level is obeyed, similarly to the default method.

In the default method, all the vectors/matrices must be atomic (see vector) or lists. Expressions are not allowed. Language objects (such as formulae and calls) and pairlists will be coerced to lists: other objects (such as names and external pointers) will be included as elements in a list result. Any classes the inputs might have are discarded (in particular, factors are replaced by their internal codes).

If there are several matrix arguments, they must all have the same number of columns (or rows) and this will be the number of columns (or rows) of the result. If all the arguments are vectors, the number of columns (rows) in the result is equal to the length of the longest vector. Values in shorter arguments are recycled to achieve this length (with a warning if they are recycled only fractionally).

关于r - 选择数据框中的下一行以便在 R 中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47959344/

相关文章:

r - 分割字符串并在每个数字之前添加分隔符

python - 使用 Pandas 和 Numpy 按 ID 索引查找比率的计算时间很长

python-3.x - python : get combinations of unique values from columns of datafame

r - devtools/GitHub 下载错误

r - data.table 中的内联 ifelse 赋值

r - 包装fortran程序以在R中使用

r - 从数据框中选择具有多列值的唯一组合的行

database - IBM-Watson 云产品中输入要求的数据格式是什么?

iOS检测数字字段的输入结束

javascript - 有人用过 Sigma Grid(基于 Javascript 的可编辑数据网格)吗?