簡體   English   中英

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

[英]Adding one new row and column in r

我想向數據框的第一列和第一行添加一個新值。

數據樣本:

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

預期成績:

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

讀取數據。 在這里,將字符串編碼為字符串(而不是作為因素)很重要:

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)

您可以使用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