简体   繁体   English

在r中添加一个新的行和列

[英]Adding one new row and column in r

I want to add a new value to the first column and first row of the data frame. 我想向数据框的第一列和第一行添加一个新值。

Sample of data: 数据样本:

  ali ata
1   u   w
2   y   e
3   t   r
4   f   x
5   s   z

Expected Results: 预期成绩:

  ali ata
1  ttt  NA
2   u   w
3   y   e
4   t   r
5   f   x
6   s   z

Read the data. 读取数据。 Here, it's important to encode strings as strings (not as factors): 在这里,将字符串编码为字符串(而不是作为因素)很重要:

df <- read.table(text="ali ata
1   u   w
2   y   e
3   t   r
4   f   x
5   s   z", header = TRUE, stringsAsFactors = FALSE)

You could use rbind to add the value: 您可以使用rbind来添加值:

rbind(c("ttt", rep(NA, ncol(df) - 1)), df)

   ali  ata
1  ttt <NA>
11   u    w
2    y    e
3    t    r
4    f    x
5    s    z

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM