如何减少不同尺寸的桌子?

时间:2017-06-12 09:29:22

标签: r

struct Question: Codable {
    var title: String
    var answer: Int
    var question: Int
}

let questionObj = Question(title: "Swift", answer: "Open Source",question:1)


let encoder = JSONEncoder()
if let encoded = try? encoder.encode(questionObj) {
    if let json = String(data: encoded, encoding: .utf8) {
        print(json)
    }

    let decoder = JSONDecoder()
    if let decoded = try? decoder.decode(Question.self, from: encoded) {
        print(decoded)
    }
}

不起作用!!!我怎样才能解决这个问题?总和不同程度的表?

2 个答案:

答案 0 :(得分:2)

您可以尝试对合并表的列进行求和:

colSums(Reduce(function(x, y) merge(x, y, all=TRUE), a), na.rm=TRUE)
#Sepal.Length Petal.Length  Petal.Width  Sepal.Width 
#        10.0          2.8          0.4          3.5 

答案 1 :(得分:1)

我们创建一个vector交叉名称并获取sum

nm1 <- Reduce(intersect,lapply(a, names))
Reduce(`+`, lapply(a, `[`, nm1))
#  Sepal.Length Petal.Length Petal.Width
#1           10          2.8         0.4

如果我们需要所有<{p>}的sum

nm2 <- Reduce(union, lapply(a, names))
Reduce(`+`,  lapply(a, function(x) { x[setdiff(nm2, names(x))] <- 0; x}))
#   Sepal.Length Sepal.Width Petal.Length Petal.Width
#1           10         4.9          1.6         0.2