r - For循环填充R中的一列
问题描述
我有一个零列和零行的数据框,我想for loop
填充从 1 到 39 的数字。这些数字应该重复两次,直到 39,例如,我正在寻找的结果将是一个列,其中每个数字重复两次
假设st
是我已经设置的数据框。这是我到目前为止所拥有的:
for(i in 1:39) {
append(st,i)
for(i in 1:39) {
append(st,i)
}
}
预期结果将采用列结构:
1
1
2
2
3
3
.
.
.
.
39
39
解决方案
你不需要使用for loop
. 而是使用rep()
# How many times you want each number to repeat sequentially
times_repeat <- 2
# Assign the repeated values as a data frame
test_data <- as.data.frame(rep(1:39, each = times_repeat))
# Change the column name if you want to
names(test_data) <- "Dont_encourage_the_use_of_blanks_in_column_names"
推荐阅读
- solr - Solr 搜索在我们搜索部分关键字时返回,但如果我们搜索实际字符串则不返回任何内容
- angular - 我映射的 observable 的结果是……一个 observable?(通过 switchMap 运行后)
- python - 为什么我必须在 python 源代码中嵌入代码版本有什么实际原因吗?
- java - 如何将应用程序映射到 docker 卷
- git - 如何在 git 存储库中获取新的子目录?
- adal-deprecation - 适用于 SharePoint Online 的 ADAL 到 MSAL
- java - Spring:嵌套实体保存空值问题
- python - 具有子组的互斥组 - argparse
- mysql - 递增表中值的连续出现次数
- c - C 语言是否需要对逻辑语句进行短路/最小评估?