Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从矩阵中提取观察值_R_Variables_Select_Matrix - Fatal编程技术网

R 从矩阵中提取观察值

R 从矩阵中提取观察值,r,variables,select,matrix,R,Variables,Select,Matrix,我有一个矩阵(见上文)。我有一排植物的种和亚种的名称 我想生成一个只有物种的矩阵和一个只有子物种的矩阵 我的初始矩阵中的物种由一个单词(abelia,abis)组成,而亚物种总是包含两个单词(abies alba,等等) 如何在R中执行此操作?假设矩阵名为m,您可以尝试以下操作: species_rows <- lengths(strsplit(rownames(m)," "))==1 #split the rownames at whitespaces, retain only rows

我有一个矩阵(见上文)。我有一排植物的种和亚种的名称

我想生成一个只有物种的矩阵和一个只有子物种的矩阵

我的初始矩阵中的物种由一个单词(
abelia
abis
)组成,而亚物种总是包含两个单词(
abies alba
,等等)


如何在R中执行此操作?

假设矩阵名为
m
,您可以尝试以下操作:

species_rows <- lengths(strsplit(rownames(m)," "))==1 #split the rownames at whitespaces, retain only rows that are not split (vector of length 1).
species_mat <- m[species_rows,] #logical subsetting
subspecies_mat <- m[!species_rows,] #logical subsetting with negation

欢迎来到,如建议,如果您提供了示例数据给您的问题,将是很好的

也就是说,我认为你可以做到:

# First, generate data:
a <- matrix(sample(c(0, 1), 20), ncol = 4)
rownames(a) <- c("abies", 
                 "abies alba", 
                 "abies amabilis", 
                 "abies balsamea", 
                 "abies concolor")
最后,分配给新矩阵:

subspecies <- a[sp,]
species <- a[-sp,]

亚种欢迎来到Stack Overflow!请通过阅读了解我们对此处问题的期望。请注意,我们这里不提供从头开始的编码服务。请告诉我们您已经尝试了什么,它是如何失败的,我们可能会提供帮助。
sp <- grep(" ", rownames(a))
subspecies <- a[sp,]
species <- a[-sp,]