我有一个数据框,其中包含不同的列,这些列是组,这些列的单元格是属于该列组的物种。我需要将其转换为二进制矩阵,其中列仍然是标题(组),但行将成为物种,如果一个物种最初在该列组中,则为1,否则为0。
我想要离开这里:
转换为这个:
谢谢!
# Load the dplyr package
library(dplyr)
# Create a list of vectors with different lengths
list_of_vectors <- list(
Z1 = c("E","F","G"),
Z2 = c("A", "B", "C", "D"),
Z3 = c("H","I","J","K","L")
)
# Find the maximum length
max_length <- max(sapply(list_of_vectors, length))
# Pad the vectors with NA to make them the same length
padded_vectors <- lapply(list_of_vectors, function(x) c(x, rep(NA, max_length - length(x))))
# Create the data frame using dplyr
df <- as.data.frame(bind_cols(padded_vectors))
我想要离开这里:
# data frame
Z1 Z2 Z3
1 E A H
2 F B I
3 G C J
4 NA D K
5 NA NA L
转换为这个:
# binary matrix
Z1 Z2 Z3
E 1 0 0
F 1 0 ...
G 1 0
A 0 1
B 0 1
C 0 1
D .. 1
H 0 1
I 1
J ...
K
L
谢谢!
data.frame(lapply(list_of_vectors, \
length<-`, max(lengths(list_of_vectors))))这段代码中,使用了
lengths(list_of_vectors)替代了
sapply(..),同时使用了
`length<-`来将
NA` 值附加到特定长度的向量中。 - undefined