我在R:
中有这行代码newDF <-oldDF[subsettingColumns]
subsettingColumns
在oldDF
中有一些列名可能存在,也可能不存在。如果它不存在,我希望在newDF
中使用NA
在同一位置插入一列。如何在R中使用它?
答案 0 :(得分:6)
我们举一个例子:
df <- data.frame(a = c(1, 2, 3, 4), b = c(4, 5, 6, 7))
df
# a b
#1 1 4
#2 2 5
#3 3 6
#4 4 7
#Columns to take subset of
subsettingColumns <- c('a', 'd', 'e')
#Columns which are already present
cols <- subsettingColumns[subsettingColumns %in% names(df)]
#Add them in the new dataframe
newdf <- df[cols]
#Assign NA to the columns which are not defined in the original dataframe
newdf[setdiff(subsettingColumns, cols)] <- NA
newdf
# a d e
#1 1 NA NA
#2 2 NA NA
#3 3 NA NA
#4 4 NA NA
答案 1 :(得分:5)
如果数据框中不存在,您可以function
添加列,如下所示:
AddColumn <- function(oldDF, subsettingColumns) {
addCol <-subsettingColumns[!subsettingColumns%in%names(oldDF)]
if(length(addCol)!=0) oldDF[addCol] <- NA
oldDF
}
在示例数据上测试此函数:
# Example data
oldDF <- data.frame(A = c(1, 2, 3, 4, 5), B = c(11, 12, 13, 14, 15))
AddColumn(oldDF, "testColumn")
# A B testColumn
#1 1 11 NA
#2 2 12 NA
#3 3 13 NA
#4 4 14 NA
#5 5 15 NA
AddColumn(oldDF, c("testColumn1", "testColumn2")
# A B testColumn1 testColumn2
#1 1 11 NA NA
#2 2 12 NA NA
#3 3 13 NA NA
#4 4 14 NA NA
#5 5 15 NA NA
答案 2 :(得分:3)
数据
oldDF <- mtcars
subsettingColumns <- c("am","IDontExist","gear","IAlsoDontExist")
获取未知列
unknownCol <- setdiff(subsettingColumns,intersect(names(mtcars),subsettingColumns))
tempDF <- lapply(unknownCol,function(x){df=data.frame(A=NA);names(df)=x;df})
oldDF <- Reduce(cbind,c(list(oldDF),tempDF))
newDF <- oldDF[subsettingColumns]
newDF
> head(newDF)
am IDontExist gear IAlsoDontExist
Mazda RX4 1 NA 4 NA
Mazda RX4 Wag 1 NA 4 NA
Datsun 710 1 NA 4 NA
Hornet 4 Drive 0 NA 3 NA
Hornet Sportabout 0 NA 3 NA
Valiant 0 NA 3 NA
>
答案 3 :(得分:1)
基于Andre Elrico答案的基本结构,您可以执行以下操作:
newDf <- data.frame(sapply(subsettingColumns,
function(x) if(x %in% names(oldDF)) oldDF[[x]] else NA))
前6行是
head(newDf)
am IDontExist gear IAlsoDontExist
1 1 NA 4 NA
2 1 NA 4 NA
3 1 NA 4 NA
4 0 NA 3 NA
5 0 NA 3 NA
6 0 NA 3 NA
数据强>
oldDF <- mtcars
subsettingColumns <- c("am","IDontExist","gear","IAlsoDontExist")