将列名转换为r中的矩阵格式
我有以下数据集:将列名转换为r中的矩阵格式,r,matrix,R,Matrix,我有以下数据集: A..B A..C B..C value 2 5 9 我想用一种方式打破它,比如我得到以下输出: A B C A 1 2 5 B 2 1 9 C 5 9 1 关于如何在r中执行此操作的想法?也许您可以尝试下面的基本r代码 dn <- strsplit(names(df), "..", fixed = TRUE) mat <- `dimnames<-`(diag(rep(1, nc
A..B A..C B..C
value 2 5 9
我想用一种方式打破它,比如我得到以下输出:
A B C
A 1 2 5
B 2 1 9
C 5 9 1
关于如何在r中执行此操作的想法?也许您可以尝试下面的基本r代码
dn <- strsplit(names(df), "..", fixed = TRUE)
mat <- `dimnames<-`(diag(rep(1, ncol(df))), replicate(2, list(unique(unlist(dn)))))
inds <- do.call(rbind, lapply(dn, function(x) rbind(x, rev(x))))
mat[inds] <- rep(unlist(df), each = 2)
资料
也许你可以试试下面的基本R代码
dn <- strsplit(names(df), "..", fixed = TRUE)
mat <- `dimnames<-`(diag(rep(1, ncol(df))), replicate(2, list(unique(unlist(dn)))))
inds <- do.call(rbind, lapply(dn, function(x) rbind(x, rev(x))))
mat[inds] <- rep(unlist(df), each = 2)
资料
带有tidyverse的选项
数据
带有tidyverse的选项
数据
下面是另一个使用矩阵索引填充值的选项:
library(splitstackshape)
# stack your dataset and split the names into two columns
x <- cSplit(stack(df), "ind", "..")
# ij is going to be your index of row and column combinations
ij <- as.matrix(x[, 2:3])
u <- unique(c(ij))
# initialze a matrix of 1s
m <- matrix(1, nrow = length(u), ncol = length(u),
dimnames = list(u, u))
# replace the relevant indices with values
m[rbind(ij, ij[, 2:1])] <- x$values
m
# A B C
# A 1 2 5
# B 2 1 9
# C 5 9 1
下面是另一个使用矩阵索引填充值的选项:
library(splitstackshape)
# stack your dataset and split the names into two columns
x <- cSplit(stack(df), "ind", "..")
# ij is going to be your index of row and column combinations
ij <- as.matrix(x[, 2:3])
u <- unique(c(ij))
# initialze a matrix of 1s
m <- matrix(1, nrow = length(u), ncol = length(u),
dimnames = list(u, u))
# replace the relevant indices with values
m[rbind(ij, ij[, 2:1])] <- x$values
m
# A B C
# A 1 2 5
# B 2 1 9
# C 5 9 1
在base中,您可以使用strsplit获取名称,使用unique获取所有级别,并创建一个用1L和级别大小初始化的矩阵。然后,您可以使用名称来填充矩阵,以查找值的位置
i <- do.call(rbind, strsplit(names(x), "..", TRUE))
u <- unique(as.vector(i))
m <- matrix(1L, length(u), length(u), dimnames = list(u, u))
m[rbind(i, i[,2:1])] <- unlist(x)
#m[rbind(i, i[,2:1])] <- x #Alternative in case of a vector
m
# A B C
#A 1 2 5
#B 2 1 9
#C 5 9 1
数据:
在base中,您可以使用strsplit获取名称,使用unique获取所有级别,并创建一个用1L和级别大小初始化的矩阵。然后,您可以使用名称来填充矩阵,以查找值的位置
i <- do.call(rbind, strsplit(names(x), "..", TRUE))
u <- unique(as.vector(i))
m <- matrix(1L, length(u), length(u), dimnames = list(u, u))
m[rbind(i, i[,2:1])] <- unlist(x)
#m[rbind(i, i[,2:1])] <- x #Alternative in case of a vector
m
# A B C
#A 1 2 5
#B 2 1 9
#C 5 9 1
数据:
对不起,我的错,应该是9。我刚刚纠正了它。谢天谢地,如果它被分类了,也许:m,还有来自。你必须把名字加进去,但要得到矩阵,你可以做librarySOfun;vec2symmatunlistdf。这个函数基本上可以归结为@GKi的建议,但是有一些额外的功能。对不起,我的不好,它应该是9。我刚刚纠正了它。谢天谢地,如果它被分类了,也许:m,还有来自。你必须把名字加进去,但要得到矩阵,你可以做librarySOfun;vec2symmatunlistdf。该功能基本上可以归结为@GKi的建议,但还有一些附加功能。回答得很好,非常感谢!回答得很好,非常感谢!
library(splitstackshape)
# stack your dataset and split the names into two columns
x <- cSplit(stack(df), "ind", "..")
# ij is going to be your index of row and column combinations
ij <- as.matrix(x[, 2:3])
u <- unique(c(ij))
# initialze a matrix of 1s
m <- matrix(1, nrow = length(u), ncol = length(u),
dimnames = list(u, u))
# replace the relevant indices with values
m[rbind(ij, ij[, 2:1])] <- x$values
m
# A B C
# A 1 2 5
# B 2 1 9
# C 5 9 1
i <- do.call(rbind, strsplit(names(x), "..", TRUE))
u <- unique(as.vector(i))
m <- matrix(1L, length(u), length(u), dimnames = list(u, u))
m[rbind(i, i[,2:1])] <- unlist(x)
#m[rbind(i, i[,2:1])] <- x #Alternative in case of a vector
m
# A B C
#A 1 2 5
#B 2 1 9
#C 5 9 1
x <- data.frame(A..B = 2L, A..C = 5L, B..C = 9L, row.names = "value")
#x <- c(A..B = 2L, A..C = 5L, B..C = 9L) #Alternative as a vector