我有一个数据框
division | category
A | tools
A | work
B | tools
B | TOOLS
两列都是因子变量。 如何将TOOLS转换为工具?
我试过
df$category <- as.character(df$category)
df$category <- lapply(df$category, function(x) { tolower(x) } )
df$category <- as.factor(df$category)
然后我得到的最后一个命令:
Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?
这是什么意思?
答案 0 :(得分:3)
错误意味着您已尝试对列表进行分析,尽管不是这些词。它被触发是因为您使用了lapply()
,它返回一个列表。在这种情况下,as.factor()
会调用factor()
,而sort.list()
会在此处调用## from factor()
if (missing(levels)) {
y <- unique(x, nmax = nmax)
ind <- sort.list(y)
...
}
:
as.factor(list(1, 2))
# Error in sort.list(y) : 'x' must be atomic for 'sort.list'
# Have you called 'sort' on a list?
这是发生错误的地方。
tolower()
长话短说,你可以使用lapply()
而不是df$category <- factor(tolower(df$category))
df
# division category
# 1 A tools
# 2 A work
# 3 B tools
# 4 B tools
,因为它是矢量化的,并为你做角色强制。
angular.module('staticSelect', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.data = {
multipleSelect: ["classA"]
};
$scope.isoffice = function(apps) {
return apps.type === $scope.data.multipleSelect[0];
};
console.log($scope.data.multipleSelect[0]);
$scope.apps = [{
"id": "1",
"type": ["classA","classB","classC"],
"name": "Name 1"
}, {
"id": "2",
"type": ["classB","classC","classE"],
"name": "Name 2"
}, {
"id": "3",
"type": ["classC"],
"name": "Name 3"
}, {
"id": "4",
"type": ["classD","classC"],
"name": "Name 4"
}, {
"id": "5",
"type": ["classA","classB","classC","classD","classE"],
"name": "Name 5"
}
];
}]).filter('multiplefilter', function() {
return function(arr , filterFrom) {
var filteredArray = [];
angular.forEach(filterFrom, function (value, key) {
angular.forEach(arr, function (arrObj, key) {
if(arrObj.type.indexOf(value) > -1 && filteredArray.indexOf(arrObj) == -1){
filteredArray.push(arrObj);
}
})
});
return filteredArray;
}
});
答案 1 :(得分:2)
我认为你不需要使用lapply。这对我有用。
{{1}}