我有一个数据框carList。它具有类型为factor的变量State,例如:
struct A{};
int a,b; // New variable a and b
struct A c,d; // New variables c,d of type struct A
// Now take a look at this:
int *px;
px = &a; // px referencing to a, no new int variable created;
px = &b; // px referencing to b, no new int variable created;
struct A* py;
py = &c; // py referencing to c, no new struct A variable created;
py = &d; // py referencing to d, no new struct A variable created;
我想对状态为“ CA”的carList进行子集
c
p = (struct A*)malloc(sizeof(struct A));
但是我没有得到结果。如何基于State作为因素对上述数据集进行子集化?
答案 0 :(得分:2)
问题可能是由于前导/滞后空格引起的。如果是这种情况,trimws
可以删除其中任何空格,然后在经过修剪的列上执行==
subset(carList, trimws(State) == "CA")
这也可以通过tidyverse
library(tidyverse)
carList %>%
filter(str_trim(State) == "CA")