首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么代码在循环时会抛出错误?当我“手动”增加索引时,我的代码就能工作,但当我放入循环时,它会失败。

为什么代码在循环时会抛出错误?当我“手动”增加索引时,我的代码就能工作,但当我放入循环时,它会失败。
EN

Stack Overflow用户
提问于 2019-07-24 00:12:25
回答 3查看 47关注 0票数 0

我希望将一个数据帧的值作为列名附加到另一个数据帧。

如果我“手动”分配索引值,我编写的代码将每次生成一个列:

代码语言:javascript
复制
df_searchtable <- data.frame(category = c("air", "ground", "ground", "air"), wiggy = c("soar", "trot", "dive", "gallop"))  
df_host <- data.frame(textcolum = c("run on the ground", "fly through the air"))

#create vector of categories
categroups <- as.character(unique(df_searchtable$category))


##### if I assign colum names one at a time using index numbers no prob:

group = categroups[1]
df_host[, group] <- NA


##### if I use a loop to assign the column names:

for (i in categroups) {
  group = categroups[i]
  df_host[, group] <- NA
}

代码失败,给出:

代码语言:javascript
复制
Error in [<-.data.frame(`*tmp*`, , group, value = NA) : 
missing values are not allowed in subscripted assignments of data frames

我怎样才能解决这个问题?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-07-24 02:14:52

下面是一个简单的基本R解决方案:

代码语言:javascript
复制
df_host[categroups] <- NA

df_host
            textcolum air ground
1   run on the ground  NA     NA
2 fly through the air  NA     NA

循环的问题在于,您正在循环每个元素,而您的代码则假设您正在循环通过1, 2, ..., n

例如:

代码语言:javascript
复制
for (i in categroups) {
  print(i)
  print(categroups[i])
}
[1] "air"
[1] NA
[1] "ground"
[1] NA

要修复循环,您可以做两件事中的一件:

代码语言:javascript
复制
for (group in categroups) {
  df_host[, group] <- NA
}

# or
for (i in seq_along(categroups)) {
  group <- categroups[i]
  df_host[, group] <- NA
}
票数 1
EN

Stack Overflow用户

发布于 2019-07-24 00:35:24

这里有一个使用map的解决方案。

代码语言:javascript
复制
bind_cols(df_host,
          map_dfc(categroups, 
                  function(group) tibble(!!group := rep(NA_real_, nrow(df_host)))))

给予:

代码语言:javascript
复制
            textcolum air ground
1   run on the ground  NA     NA
2 fly through the air  NA     NA
  • map_dfc映射到输入categroups上,为每个输入categroups创建一个单列tibble,并将新创建的tibbles连接到一个dataframe中。
  • bind_cols将原始数据加入到新的tibble中。

或者,您可以使用walk

代码语言:javascript
复制
walk(categroups, function(group){df_host <<- mutate(df_host, !!group := rep(NA_real_, nrow(df_host)))})
票数 1
EN

Stack Overflow用户

发布于 2019-07-24 00:41:17

下面是一个丑陋的基本R解决方案:创建一个带有列名的空矩阵,并将其cbind到第二个dataframe。

代码语言:javascript
复制
df_searchtable <- data.frame(category = c("air", "ground", "ground", "air"), 
                             wiggy = c("soar", "trot", "dive", "gallop"),
                             stringsAsFactors = FALSE)

df_host <- data.frame(textcolum = c("run on the ground", "fly through the air"),
                      stringsAsFactors = FALSE)

cbind(df_host, 
      matrix(nrow = nrow(df_host), 
             ncol = length(unique(df_searchtable$category)), 
             dimnames = list(NULL, unique(df_searchtable$category))))

结果:

代码语言:javascript
复制
            textcolum air ground
1   run on the ground  NA     NA
2 fly through the air  NA     NA
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57173657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档