Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:查找缺少的列,如果缺少,则添加到数据帧_R - Fatal编程技术网

R:查找缺少的列,如果缺少,则添加到数据帧

R:查找缺少的列,如果缺少,则添加到数据帧,r,R,我想写一些代码,这些代码将采用给定的数据帧,检查是否缺少任何列,如果缺少,则添加用0或NA填充的缺少的列。以下是我得到的: > df x1 x2 x4 1 0 1 3 2 3 1 3 3 1 2 1 > nameslist <- c("x1","x2","x3","x4") > miss.names <- !nameslist %in% colnames(df) > holder <- rbind(nameslist,mi

我想写一些代码,这些代码将采用给定的数据帧,检查是否缺少任何列,如果缺少,则添加用0或NA填充的缺少的列。以下是我得到的:

> df
   x1 x2 x4
1   0  1  3
2   3  1  3
3   1  2  1

> nameslist <- c("x1","x2","x3","x4")
> miss.names <- !nameslist %in% colnames(df)
> holder <- rbind(nameslist,miss.names)
> miss.cols <- subset(holder[1,], holder[2,] == "TRUE")
>df
x1x2x4
1   0  1  3
2   3  1  3
3   1  2  1

>名称列表miss.names holder miss.cols这里有一个简单的方法

df <- data.frame(a=1:4, e=4:1)
nms <- c("a", "b", "d", "e")   # Vector of columns you want in this data.frame

Missing <- setdiff(nms, names(df))  # Find names of missing columns
df[Missing] <- 0                    # Add them, filled with '0's
df <- df[nms]                       # Put columns in desired order
#   a b d e
# 1 1 0 0 4
# 2 2 0 0 3
# 3 3 0 0 2
# 4 4 0 0 1
df
库(stringr)
df-df
x1x2x5
1  1  1  1
2  2  2  2
3  3  3  3
4  4  4  4

当前谢谢大家,谢谢你们,我用一个数据帧列表(文件)和另一个colnames列表(ncolus)做到了这一点


你也可以使用
Missing@HongOoi——很好的建议。这就更好了,我已经编辑了答案,将其包括在内。谢谢
library(stringr)
df <- data.frame(X1=1:4,X2=1:4,X5=1:4)
>df
  X1 X2 X5
1  1  1  1
2  2  2  2
3  3  3  3
4  4  4  4
current <- as.numeric(str_extract(names(df),"[0-9]"))
missing <-seq(min(current),max(current))

df[paste("X",missing[!missing %in% current],sep="")]<-0

>df[,order(colnames(df))]
  X1 X2 X3 X4 X5
1  1  1  0  0  1
2  2  2  0  0  2
3  3  3  0  0  3
4  4  4  0  0  4
 for (i in serieI)    {
     if ((identical(colnames(Files[[i]]),ncolunas)) == FALSE) {

         nms   = ncolunas
          df =   Files[[i]]
          aux = colnames(df)
          aux1 = row.names(df)

          Missing = setdiff(nms, colnames(df))  

          serie = seq(1,length(Missing)) #creating indices 1-5 for loop
          for (j in serie)  {    #loop to add colums with zeros
              df = cbind(df,c(0))
          }
          colnames(df) = c(aux,Missing)   #updates columns names

          df = df[,order(colnames(df))]  #put colums into order
          df = t(as.matrix(df))          #hanges into matrix
           row.names(df) = aux1          #update lines' names
           Files[[i]] = df              #updates object from list
     } 

 }