子集矩阵,寻址 colnames
Subsetting a matrix, addressing colnames
我有来自包 R{tm} 的文档术语矩阵,我已将其强制转换为 as.matrix
。 MWE 在这里:
> inspect(dtm[1:ncorpus, intersect(colnames(dtm), thai_list)])
<<DocumentTermMatrix (documents: 15, terms: 4)>>
Non-/sparse entries: 17/43
Sparsity : 72%
Maximal term length: 12
Weighting : term frequency (tf)
Terms
Docs toyota_suv gmotors_suv ford_suv nissan_suv
1 0 1 0 0
2 0 1 0 0
3 0 1 0 0
4 0 2 0 0
5 0 4 0 0
6 1 1 0 0
7 1 1 0 0
8 0 1 0 0
9 0 1 0 0
10 0 1 0 0
我需要对这个 as.matrix(dtm)
进行子集化,这样我只能得到引用 toyota_suv
但没有其他车辆的文档(行)。我使用 dmat<-as.matrix(dtm[1:ncorpus, intersect(colnames(dtm), "toyota_suv")])
得到一个术语 (toyota_suv) 的子集,效果很好。如何设置查询:toyota_suv 非零但非 toyota_suv 列的值为零的文档?我可以按列指定为 ==0
但此矩阵是动态生成的。在某些市场中,可能有四辆汽车,在某些市场中可能有十辆。我不能事先指定 colnames。我如何(动态地)将所有非 toyota_suv 列合并为零,例如 all_others==0?
任何帮助将不胜感激。
如果您提供您所用的确切代码和要使用的样本数据集,那将很有帮助,这样我们就可以复制您的工作并提供一个工作示例。
鉴于此,如果我正确理解您的问题,您正在寻找一种方法将所有非丰田列标记为零。你可以试试:
df[colnames(df) != "toyota"] <- 0
您可以通过获取 toyota_suv
的索引位置,然后子集化 dtm
来匹配非零的索引位置,以及对同一索引变量使用负索引的所有其他列来完成此操作确保它们都为零。
这里我稍微修改了你的 dtm
以便 toyota_sub
非零的两种情况符合你正在寻找的标准(因为你的例子中的 none 符合它们) :
dtm <- read.table(textConnection("
toyota_suv gmotors_suv ford_suv nissan_suv
0 1 0 0
0 1 0 0
0 1 0 0
0 2 0 0
0 4 0 0
1 0 0 0
1 0 0 0
0 1 0 0
0 1 0 0
0 1 0 0"), header = TRUE)
然后就可以了:
# get the index of the toyota_suv column
index_toyota_suv <- which(colnames(dtm) == "toyota_suv")
# select only cases where toyota_suv is non-zero and others are zero
dtm[dtm[, "toyota_suv"] > 0 & !rowSums(dtm[, -index_toyota_suv]), ]
## toyota_suv gmotors_suv ford_suv nissan_suv
## 6 1 0 0 0
## 7 1 0 0 0
注意:这根本不是一个真正的文本分析问题,而是一个关于如何对矩阵对象进行子集化的问题。
我有来自包 R{tm} 的文档术语矩阵,我已将其强制转换为 as.matrix
。 MWE 在这里:
> inspect(dtm[1:ncorpus, intersect(colnames(dtm), thai_list)])
<<DocumentTermMatrix (documents: 15, terms: 4)>>
Non-/sparse entries: 17/43
Sparsity : 72%
Maximal term length: 12
Weighting : term frequency (tf)
Terms
Docs toyota_suv gmotors_suv ford_suv nissan_suv
1 0 1 0 0
2 0 1 0 0
3 0 1 0 0
4 0 2 0 0
5 0 4 0 0
6 1 1 0 0
7 1 1 0 0
8 0 1 0 0
9 0 1 0 0
10 0 1 0 0
我需要对这个 as.matrix(dtm)
进行子集化,这样我只能得到引用 toyota_suv
但没有其他车辆的文档(行)。我使用 dmat<-as.matrix(dtm[1:ncorpus, intersect(colnames(dtm), "toyota_suv")])
得到一个术语 (toyota_suv) 的子集,效果很好。如何设置查询:toyota_suv 非零但非 toyota_suv 列的值为零的文档?我可以按列指定为 ==0
但此矩阵是动态生成的。在某些市场中,可能有四辆汽车,在某些市场中可能有十辆。我不能事先指定 colnames。我如何(动态地)将所有非 toyota_suv 列合并为零,例如 all_others==0?
任何帮助将不胜感激。
如果您提供您所用的确切代码和要使用的样本数据集,那将很有帮助,这样我们就可以复制您的工作并提供一个工作示例。
鉴于此,如果我正确理解您的问题,您正在寻找一种方法将所有非丰田列标记为零。你可以试试:
df[colnames(df) != "toyota"] <- 0
您可以通过获取 toyota_suv
的索引位置,然后子集化 dtm
来匹配非零的索引位置,以及对同一索引变量使用负索引的所有其他列来完成此操作确保它们都为零。
这里我稍微修改了你的 dtm
以便 toyota_sub
非零的两种情况符合你正在寻找的标准(因为你的例子中的 none 符合它们) :
dtm <- read.table(textConnection("
toyota_suv gmotors_suv ford_suv nissan_suv
0 1 0 0
0 1 0 0
0 1 0 0
0 2 0 0
0 4 0 0
1 0 0 0
1 0 0 0
0 1 0 0
0 1 0 0
0 1 0 0"), header = TRUE)
然后就可以了:
# get the index of the toyota_suv column
index_toyota_suv <- which(colnames(dtm) == "toyota_suv")
# select only cases where toyota_suv is non-zero and others are zero
dtm[dtm[, "toyota_suv"] > 0 & !rowSums(dtm[, -index_toyota_suv]), ]
## toyota_suv gmotors_suv ford_suv nissan_suv
## 6 1 0 0 0
## 7 1 0 0 0
注意:这根本不是一个真正的文本分析问题,而是一个关于如何对矩阵对象进行子集化的问题。