简体   繁体   English

更新 R 中 dataframe 中单元格的值

[英]update the value of a cell in a dataframe in R

I am trying to fill the values of a variable row by row for which I loop through the dataframe and when I have the value I want to fill I do this::我正在尝试逐行填充变量的值,为此我循环遍历 dataframe 并且当我有要填充的值时,我这样做:

listadoProyectosPlanificados<-function(df){
variables<-names(df)
variablesAIgnorar<-c('Total','JefeProyecto','Ranking','Proyectos')
variablesATenerEncuenta<-subset(variables,!(variables %in% variablesAIgnorar))
for (indiceFila in 1:nrow(df)) {
 proyectos<-vector()
 for (indiceColumna in 1:length(variablesATenerEncuenta)) {
  if (df[indiceFila,variablesATenerEncuenta[indiceColumna]]!=0) {
    proyectos<-append(proyectos,variablesATenerEncuenta[indiceColumna])
  }
}
print(indiceFila)
print(ncol(df))
print(paste(proyectos,collapse = ', '))
df[indiceFila,ncol(df)]<-paste(proyectos,collapse = ', ')
}
}

印刷

But this sentence do nothing in df但是这句话在df中什么都不做

df[indiceFila,ncol(df)]<-paste(proyectos,collapse = ', ')

but if I do this in consle df[1,49]<-paste(c('hola','adios'),collapse = ', ')但是如果我在控制台中执行此操作 df[1,49]<-paste(c('hola','adios'),collapse = ', ')

> df[1,49]
[1] "hola, adios"

it updates它更新

any idea, please?有什么想法吗?

regards问候

Change MyDataFrame to the name of your data frame and it should work.MyDataFrame更改为您的数据框的名称,它应该可以工作。

listadoProyectosPlanificados <- function(df){
  variables<-names(df)
  variablesAIgnorar<-c('Total','JefeProyecto','Ranking','Proyectos')
  variablesATenerEncuenta<-subset(variables,!(variables %in% variablesAIgnorar))
  for (indiceFila in 1:nrow(df)) {
    proyectos<-vector()
    for (indiceColumna in 1:length(variablesATenerEncuenta)) {
      if (df[indiceFila,variablesATenerEncuenta[indiceColumna]]!=0) {
        proyectos<-append(proyectos,variablesATenerEncuenta[indiceColumna])
      }
    }
    print(indiceFila)
    print(ncol(df))
    print(paste(proyectos,collapse = ', '))
    df[indiceFila,ncol(df)]<-paste(proyectos,collapse = ', ')
  }

  return( df ) #new statement
}

MyDataFrame <- listadoProyectosPlanificados(MyDataFrame)

暂无
暂无

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

相关问题 在 R dataframe 中查找值的单元格引用 - Find a cell reference for a value in a R dataframe 根据该行中另一个单元格的值更改 dataframe 单元格中的值 (R) - Changing a value in a dataframe cell based on the value of another cell in that row (R) R - 连接 dataframe 中的单元格,按组,取决于另一个单元格值 - R - Concatenate cell in dataframe, by group, depending on another cell value R:在 dataframe 单元格搜索中插入值,从另一个 dataframe 中提取它 - R: insert value in dataframe cell searching extracting it from another dataframe 在 R - 如果单元格值与列匹配,则返回值,否则在新的 dataframe 中为 0 - In R - if cell value matches a column, return value, else 0 in a new dataframe 通过在 R 中添加其他单元格和行的值来扩展 Dataframe 中的值 - Extend Value in Dataframe by add Value of an other Cell and Row in R 根据 R 中某一列中的值获取 dataframe 中某个单元格的值 - get the value of a cell of a dataframe based on the value in one of the columns in R 根据单元格中的值(在 R 中!)有条件地在行中拆分数据帧 - Conditionally split dataframe in rows according to value in cell (in R!) 如何使用 tidyverse 重新评估 r 中的 dataframe 中更改的单元格值 - how to reassing a changed cell value back in a dataframe in r with the tidyverse R:如何将 dataframe 中的一行拆分为多行,以单元格中的值为条件? - R: How to split a row in a dataframe into a number of rows, conditional on a value in a cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM