简体   繁体   English

如何基于R中的匹配值将值列表插入到更大的列中

[英]How can I insert a list of values into a larger column based on matching values in R

if I have a preallocated array ie" 如果我有一个预分配的数组,即“

[1] [2] [3] [4] [5] [6]

A  

B

C

D

E

F

..

..

Z

how can I insert lists of differing but shorter lengths ie (B,D,E,F) or (A,C,EG,J,K) in to the columns [2] : [6] ? 如何在[2] : [6]列中插入长度不同但较短的列表,例如(B,D,E,F)(A,C,EG,J,K)

Thanks, 谢谢,

sapply(newarray, function(elem) { master_list[,2] <- rbind(master_list[,2], elem})

elem is the parameter to the anonymous function, passed as the second argument to sapply . elem是匿名函数的参数,作为第二个参数传递给sapply Look in the help for more details. 在帮助中查找更多详细信息。

My rationale for these sorts of problems is to reduce everything to a single assignment. 对于此类问题,我的基本原理是将所有内容简化为一个任务。 In the case of matrices, you can index them using another matrix to achieve this, like so: 对于矩阵,可以使用另一个矩阵为它们建立索引,如下所示:

Set up a test array and vectors: 设置测试数组和向量:

arr <- matrix(NA,nrow=5,ncol=6)
arr[,1] <- LETTERS[1:5]
vec1 <- c("B","D","E")
vec2 <- c("A","C","E","G")

#     [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] "A"  NA   NA   NA   NA   NA  
#[2,] "B"  NA   NA   NA   NA   NA  
#[3,] "C"  NA   NA   NA   NA   NA  
#[4,] "D"  NA   NA   NA   NA   NA  
#[5,] "E"  NA   NA   NA   NA   NA 

Figure out matrix columns to assign to: 找出要分配给的矩阵列:

vecs <- list(vec1,vec2)
lens <- sapply(vecs,length)
mat <- cbind(sequence(lens),rep(2:3,lens))
mat

#     row   col    
#     [,1] [,2]
#[1,]    1    2
#[2,]    2    2
#[3,]    3    2
#[4,]    1    3
#[5,]    2    3
#[6,]    3    3
#[7,]    4    3

Assign away: 分配:

arr[mat] <- unlist(vecs)
arr
#     [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] "A"  "B"  "A"  NA   NA   NA  
#[2,] "B"  "D"  "C"  NA   NA   NA  
#[3,] "C"  "E"  "E"  NA   NA   NA  
#[4,] "D"  NA   "G"  NA   NA   NA  
#[5,] "E"  NA   NA   NA   NA   NA  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据R中的匹配ID添加列值? - How do I add column values based on matching IDs in R? 如何基于匹配R中的其他列的行值来填充列的值 - How do I fill in values for columns based on matching few other column's row values in R 如何根据满足特定条件的所有行过滤具有匹配列值的多行? [R] - How do I filter multiple rows with matching column values based on all rows meeting a certain condition? [R] 如何根据其他 3 列 [R] 中的匹配值计算我在 1 列中求和的行数? - How do I count the number of rows I have summed values in 1 column based on matching values in 3 other columns [R]? 如何合并具有不同大小的匹配列值的数据框列表 - How can I merge list of dataframes of different size matching column values 如何在R中的列匹配模式中转换值 - How to convert values in column matching pattern in R 如何在 R 中多次根据另一列的类别对 1 列的值求和? - How can i sum values of 1 column based on the categories of another column, multiple times, in R? R,如何根据多个条件在列表列中累积值 - R, How to accumulate values in a list column, based on multiple criteria 如何根据先前的列在列中插入/替换值? - How can I insert/replace values within a column based on preceding columns? R:使用列表列的匹配值列表过滤数据框 - R: Filter a Dataframe with a list on matching values of a list-column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM