根据列表

时间:2017-12-01 21:07:43

标签: r

我有25个像这样的列表形式的数据框(这里显示了三个,三个足以用于样本计算):

df1 <- data.frame(c(1, 3, 2, 4, 2, 2,4), c(4, 5, 2, 5, 6, 3, 2))
df2 <- data.frame(c(4, 2, 5, 2, 5, 2, 6), c(6, 3, 2, 6, 2, 6, 3))
df3 <- data.frame(c(1, 3, 2, 4, 2, 2,4), c(4, 5, 2, 5, 6, 3, 2))
list <- list(df1, df2, df3)

我想制作另一个数据框,其中包含每个单元格中的平均值,然后我想要标准偏差。因此它将是一个4列乘7行的数据帧,其中包含数据帧中每个单元的平均值和标准差。

如何做到这一点?我的数据也超过了7行。

2 个答案:

答案 0 :(得分:2)

我承认d.b的回答给我留下了深刻印象,但我认为通过将列表转换为3d数组可以更好地解决这个问题。

struct AccessInfo : Decodable
{
    let token: String
    let permission: [String]
    let timeout: Int
    let issuer: String
    let additionalData: [String: Any]

    private enum CodingKeys: String, CodingKey
    {
        case token
        case permission
        case timeout = "timeout_in"
        case issuer
    }

    public init(from decoder: Decoder) throws
    {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        token = container.decode(String.self, forKey: .token)
        permission = try container.decodeIfPresent(String.self, forKey: .permission).components(separatedBy: ",")
        timeout = try container.decode(Int.self, forKey: . timeout)
        issuer = container.decode(String.self, forKey: .issuer)

        // This is where I'm stuck, how do I add the remaining
        // unknown JSON elements into additionalData?
    }
}

// Calling code, breviated for clarity
let decoder = JSONDecoder()
let accessInfo = try decoder.decode(AccessInfo.self, from: data!)

<强>结果

vector<IComponent*> myComponents; //assume it has items in it already.
void RemoveComponent(IComponent* componentToRemove)
{
    IComponent* juggler;

    if (componentToRemove != NULL)
    {
        for (int currComponentIndex = 0; currComponentIndex < myComponents.size(); currComponentIndex++)
        {
            if (componentToRemove == myComponents[currComponentIndex])
            {
                //Since we don't care about order, swap with the last element, then delete it.
                juggler = myComponents[currComponentIndex];
                myComponents[currComponentIndex] = myComponents[myComponents.size() - 1];
                myComponents[myComponents.size() - 1] = juggler;

                //Remove it from memory and let the vector know too.
                myComponents.pop_back();
                delete juggler;
            }
        }
    }
}

答案 1 :(得分:1)

mylist <- list(df1, df2, df3)
do.call(cbind, lapply(lapply(1:2, function(i) sapply(mylist, function(x) x[,i])),
                      function(a) t(apply(a, 1, function(x) c(mean(x), sd(x))))))
#         [,1]      [,2]     [,3]      [,4]
#[1,] 2.000000 1.7320508 4.666667 1.1547005
#[2,] 2.666667 0.5773503 4.333333 1.1547005
#[3,] 3.000000 1.7320508 2.000000 0.0000000
#[4,] 3.333333 1.1547005 5.333333 0.5773503
#[5,] 3.000000 1.7320508 4.666667 2.3094011
#[6,] 2.000000 0.0000000 4.000000 1.7320508
#[7,] 4.666667 1.1547005 2.333333 0.5773503