【问题标题】:How to add a column with constant observation and another variable with consecutive numbers with a character in R [closed]如何在 R 中添加具有恒定观察值的列和具有连续数字的另一个变量 [关闭]
【发布时间】:2016-09-10 08:28:18
【问题描述】:

我想在现有数据框中添加带有字符的连续数字的第一列。

我使用以下代码。它不起作用。

df$VARNAME_ <- paste0('COL', 1:5)(df) 

我希望它看起来像这样。

VARNAME_ old_var1 old_var2
   COL1         1        2
   COL2         1        2
   COL3         1        2
   COL4         1        2
   COL5         1        2

提前致谢。


对不起,我问了一个愚蠢的问题。现在我想通了。 解决方法如下。

actual_df<-data.frame(df)#transfer matrix a to data frame
actual_df<-cbind(VARNAME_=paste0('COL', 1:5),actual_df) #add COL1~COL5 in the first column
actual_df<-cbind(ROWTYPE_ = 'PROX', actual_df) #Add a variable with constant observations in first column. Now the previous column become second one.

【问题讨论】:

  • df$VARNAME_ &lt;- paste0('COL', 1:5) 本身应该可以工作吗?
  • 它显示“在 $VARNAME_
  • 我对此没有任何问题。不确定。
  • 哦!有用!!谢谢你。我需要先将我的矩阵转移到数据框。然后,我使用您提到的 'cbind' 将 VARNAME_ 移动到第一列。非常感谢。

标签: r variables dataframe


【解决方案1】:
df$VARNAME_ = paste0('COL', 1:5)

会起作用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-01
    • 2021-01-08
    • 1970-01-01
    • 2019-05-01
    • 2023-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多