我有一个包含三列的数据框: ref,target,distance。每个ref都有一个到同一组目标的测量距离,我想为每个ref得到一个最小距离的向量。现在我正在使用for循环来做这件事,但似乎应该有一种方法来向量化这一点。
下面是我的代码:
refs <- levels(data$ref)
result <- c()
for (ref in refs) {
# Find the minimum distance for observations with the current ref
# but be sure to protect against ref ==
我正在尝试为基于训练中心城市的健身测试数据创建自动化的PDF RMarkdown报告。 我相信通过遵循这里的大纲,我已经非常接近了:R Knitr PDF: Is there a posssibility to automatically save PDF reports (generated from .Rmd) through a loop? 然而,尽管两个集线器的命名不同(report.A.pdf和report.B.pdf),但这只为一个集线器创建了具有相同数据的报告。如何让子组正确循环以显示来自不同集线器的数据? 示例数据: Date Athlete
对于给定的数据帧,我想将数组的值乘以数据帧的一列。数据框由行组成,其中包含一个名称、一个数值和两个因子值:
name credit gender group
n1 10 m A
n2 20 f B
n3 30 m A
n4 40 m B
n5 50 f C
可以使用以下命令生成此数据帧:
name <- c('n1','n2','n3','n4','n5')
credit <- c(10,20,30,40,50)
gender <- c('m','f',&
int a = 0, b = 0;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
a = a + j;
}
}
for (k = 0; k < N; k++) {
b = b + k;
}
我正试图计算出上述问题的时间复杂性。
我以为是O(n^2 + n),我的推理是:
n^2 : nested for loops
n : Adding the single loop
然而,确定的答案是O(n
我有一个dataframe,它包含了网络中所有节点的连接,我想要创建一个名为“节点”的新数据帧,它包含所有唯一的节点。我试着做这样的事情
eids<-as.factor(d$from)
mids<-as.factor(d$to)
nodes<-data.frame(c(eids,mids))
nodes<-unique(nodes)
但是当我试图创建我得到的图形:Some vertex names in edge list are not listed in vertex data frame时,这意味着我的部分数据被这个方法遗漏了。我的数据集很大,所以我在这里放了一个
我正在尝试组合一个函数,它将循环遍历给定的数据帧,并返回一个新的数据帧,其中包含从原始数据帧计算出的内容。X的长度每次都会不同,实际问题在函数中会有更多的循环。对R来说是新手,并且还没有找到任何有用的东西(我不认为使用列表会有帮助)
func<-function(x){
tmp # need to declare this here?
for (i in 1:dim(x)[1]){
tmp[i]<-ave(x[i,]) # add things to it
}
return(tmp)
}
df<-cbind(rnorm(10
从下面的数据集中:
Referee Tca
1 A D'Urso 2.733333
2 A Davies 3.529412
3 A Haines 2.722222
4 A Madley 3.407407
5 A Marriner 3.640000
6 A Taylor 3.603448
7 A Woolmer 3.222222
8 B Malone 1.636364
9 C Berry 3.800000
10 C Boyeson 3.800000