所以我希望将第14:18列改为1栏"输入"。我想给这个新列中的每个条目(用于匹配前面的观察)5中的哪个值是1(因为它们中只有1个可以为真)。这是我在R中做到这一点的最佳尝试(并且超越了沮丧)。
document.addEventListener("DOMContentLoaded", function(event) {
var acc = document.getElementsByClassName("accordion");
var panel = document.getElementsByClassName('panel');
for (var i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
var setClasses = !this.classList.contains('active');
setClass(acc, 'active', 'remove');
setClass(panel, 'show', 'remove');
if (setClasses) {
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
}
function setClass(els, className, fnName) {
for (var i = 0; i < els.length; i++) {
els[i].classList[fnName](className);
}
}
});
这就是我想要的......但是,我也希望我的其余数据框架能够随之而来,就像我只想要新的#34; Type&#34;但我甚至无法使用以下代码行访问它......
library(caret)
data("cars")
carSubset <- subset(cars)
head(carSubset)
# I want to convert the columns from of carSubset with following vector names
types <- c("convertible","coupe", "hatchback", "sedan", "wagon")
# into 1 column named Type, with the corresponding column name
carSubset$Type <- NULL
carSubset <- apply(carSubset[,types],
2,
function(each_obs){
hit_index <- which(each_obs == 1)
carSubset$Type <- types[hit_index]
})
head(carSubset) # output:
1 2 3 4 5
"sedan" "coupe" "convertible" "convertible" "convertible"
有关如何动态添加新列,同时将以前相关的数据观察结果添加到其中的任何帮助?
答案 0 :(得分:0)
我真的想通了!可能不是最好的方法,但嘿,它有效。
head(carSubset$Type) # output: Error in carSubset$Type : $ operator is invalid for atomic vectors