我有一个数据框x,其列名具有相似的前缀(age_1,age_2,age_3,...,age_n)。我想删除前缀为'age_'且全部为NA的列。在下面的示例中,它将是age_2和age_4。但在我的数据集中,它可能会达到age_100。有没有办法做到这一点?
示例数据框:
def userInput = input (message: 'Approve Delete', submitterParameter: 'isApproved')
echo ("userInput was: " + userInput)
if(userInput.equals("Yes"))
{
job(env.JOB_NAME) {
steps {
dsl {
removeAction('DELETE')
}
}
}
}
else
{
echo("No Approval received to delete Job")
}
示例输出:
x <- data.frame("age_1" = c(0,1,1,0), "age_2" = NA , "age_3" = c(1,0,0,0), "age_4"=NA, "name_1" = NA, "name_2" = NA)
答案 0 :(得分:2)
您希望通过两个条件进行选择:列名称以age_
开头,其所有元素均为NA。您可以通过以下方式选择这些列:
sel = grepl("^age_",colnames(x), ignore.case = T) & sapply(x, FUN = function(x){all(is.na(x))})
然后执行:
new_x = x[,!sel]
结果
age_1 age_3 name_1 name_2
1 0 1 NA NA
2 1 0 NA NA
3 1 0 NA NA
4 0 0 NA NA
grepl(...)
仅适用于那些以age_
sapply(...)
将遍历每一列,并在all
元素为NA(is.na()
)时返回true。
两者的交集是您需要忽略的(sel
否定!
)。
答案 1 :(得分:0)
y_1 <- x %>% select(which(!(names(.) %in% grep("^[age]", names(.), value = T))))
y_2 <- x[colSums(!is.na(x)) > 0]
y <- bind_cols(y_1,y_2)
不优雅但有效
答案 2 :(得分:0)
另一种选择是使用import simplejson
class C:
def __init__(self, item):
self.item = item
class json_encoder(simplejson.JSONEncoder):
def default(self, obj):
print("WooWoo! Called!", obj)
if isinstance(obj, C):
return obj.item
raise TypeError(repr(obj) + " is not JSON serializable")
with open('save.json', 'w') as outfile:
outfile.write(simplejson.dumps([1,C(47),C('orange'),4], cls=json_encoder))
和colSums
grepl