我需要在R中构建一个包含viewDidLoad
(或@media screen and (max-width:768px) {
button.icon{
min-width:50%;
max-width:700px;
margin:2px 0;
}
}
)的数据结构。理想情况下,它是matrix
内的data.frame
。到目前为止,我只能想到嵌套列表来实现它,但是我害怕性能不佳。
例如,对于data.frame元素
matrix
我想在每个单元格中添加一个包含矩阵的列(由距离函数生成)。例如,对于元素start ==“A”,end ==“B”,它可以是矩阵(或data.frame)
data.frame
理论上,它只是某种3d数据结构。在Python中,它将是包含df <- data.frame(start=c("A", "B", "C"), end=c("A", "B", "C"))
- 数组的列表列表。这样的事情可能在R?
我想用自定义距离函数执行knn,我需要在执行haversineStart haversineEnd tripLengthDiff startCountry endCountry truckDiff
160.5408 308.1947 198.745 1 1 1
152.4168 308.1947 20.710 1 1 1
273.7599 2228.3508 2903.212 0 1 1
之前规范化距离
答案 0 :(得分:1)
如果您已经有嵌套列表:
d <- list(
a = list(matrix(rnorm(4), 2, 2), matrix(rnorm(4), 2, 2), matrix(rnorm(4), 2, 2)),
b = list(matrix(rnorm(4), 2, 2), matrix(rnorm(4), 2, 2), matrix(rnorm(4), 2, 2))
)
你可以很容易地将它转换为data.frame,因为data.frame仍然是列表:
class(d) <- 'data.frame'
colnames(d) <- c('A', 'B')
rownames(d) <- c('A', 'B', 'C')
d['A', 'B']
# [[1]]
# [,1] [,2]
# [1,] -0.6326935 -1.1181986
# [2,] -1.3066515 0.6672159
答案 1 :(得分:1)
只需将矩阵列表分配给data.frame中的新列即可。例如,假设df
来自问题:
m <- matrix(c(1, 12, 3, 14), 2)
df$mat <- list(m, 2*m, 3*m) # test list
所以
> df$mat[[1]]
[,1] [,2]
[1,] 1 3
[2,] 12 14
> df[[1, "mat"]]
[,1] [,2]
[1,] 1 3
[2,] 12 14
> transform(df, det = sapply(mat, det))
start end mat det
1 A A 1, 12, 3, 14 -22
2 B B 2, 24, 6, 28 -88
3 C C 3, 36, 9, 42 -198