我正在尝试将两个长度不等的列表组合到一个数据框中。
我从包含2个矩阵的列表开始。
mtx_a <- matrix(data = c(1:24), nrow = 4)
mtx_b <- matrix(data = c(25:36), nrow = 3)
row.names(mtx_a) <- c("A", "B", "C", "D")
row.names(mtx_b) <- c("A", "B", "C")
colnames(mtx_a) <- c("V1", "V2", "W1", "W2", "X1", "X2")
colnames(mtx_b) <- c("Y1", "Y2", "Z1", "Z2")
mtx_list <- list(mtx_a, mtx_b)
names(mtx_list) <- c("mtx_a", "mtx_b")
mtx_list
我根据rowname提取了一个特定的行进行检查。
mtx_list <- lapply(mtx_list, function(mtx_list){
mtx_list[row.names(mtx_list) %in% c("A"),]})
接下来,我根据索引提取特定列。 (交替列等同于条件1和2)。
cond_1 <- lapply(lapply(mtx_list, function(mtx_list) {
mtx_list[c(T,F)]}), unname)
cond_2 <- lapply(lapply(mtx_list, function(mtx_list) {
mtx_list[c(F,T)]}), unname)
我正在尝试生成一个如下所示的数据框:
cond_1 cond_2
mtx_a 1 5
mtx_a 9 13
mtx_a 17 21
mtx_b 25 28
mtx_b 31 34
我尝试了以下但没有取得任何成功:
list(cond_1, cond_2)
library(plyr)
ldply(c(cond_1, cond_2))
答案 0 :(得分:4)
首先提取所有"A"
行,然后直接输入2列矩阵:
matrix(unlist(lapply(mtx_list, `[`, "A", )), ncol=2, byrow=TRUE)
# [,1] [,2]
#[1,] 1 5
#[2,] 9 13
#[3,] 17 21
#[4,] 25 28
#[5,] 31 34
如果需要,可以将其包含在data.frame()
中。
答案 1 :(得分:2)
您可以使用import aiohttp
import asyncio
import async_timeout
from unittest import TestCase
from mocket.mocket import mocketize
from mocket.mockhttp import Entry
class AioHttpEntryTestCase(TestCase):
@mocketize
def test_http_session(self):
url = 'http://httpbin.org/ip'
body = "asd" * 100
Entry.single_register(Entry.GET, url, body=body, status=404)
Entry.single_register(Entry.POST, url, body=body*2, status=201)
async def main(l):
async with aiohttp.ClientSession(loop=l) as session:
with async_timeout.timeout(3):
async with session.get(url) as get_response:
assert get_response.status == 404
assert await get_response.text() == body
with async_timeout.timeout(3):
async with session.post(url, data=body * 6) as post_response:
assert post_response.status == 201
assert await post_response.text() == body * 2
loop = asyncio.get_event_loop()
loop.set_debug(True)
loop.run_until_complete(main(loop))
@mocketize
def test_https_session(self):
url = 'https://httpbin.org/ip'
body = "asd" * 100
Entry.single_register(Entry.GET, url, body=body, status=404)
Entry.single_register(Entry.POST, url, body=body*2, status=201)
async def main(l):
async with aiohttp.ClientSession(loop=l) as session:
with async_timeout.timeout(3):
async with session.get(url) as get_response:
assert get_response.status == 404
assert await get_response.text() == body
with async_timeout.timeout(3):
async with session.post(url, data=body * 6) as post_response:
assert post_response.status == 201
assert await post_response.text() == body * 2
loop = asyncio.get_event_loop()
loop.set_debug(True)
loop.run_until_complete(main(loop))
到Map
相应的列表元素。
cbind
多一点工作可以给你稍微好一点的结果
do.call(rbind, Map(cbind, cond_1, cond_2))
# [,1] [,2]
#[1,] 1 5
#[2,] 9 13
#[3,] 17 21
#[4,] 25 28
#[5,] 31 34
答案 2 :(得分:2)
以下是使用sapply
的替代方法。我将它包装在一个函数中,以便于重复使用。
getter <- function(rowName) {
# loop through matrices in list, pull out rows that match name
temp <- unlist(sapply(mtx_list, function(x) x[rowName,]), use.names=FALSE)
# return data.frame with alternating values
data.frame(var1=temp[c(TRUE, FALSE)], var2=temp[c(FALSE, TRUE)])
}
getter("A")
var1 var2
1 1 5
2 9 13
3 17 21
4 25 28
5 31 34
请注意,列表中的所有矩阵都必须包含此函数的行名称。