我有df类型和价值观.我想按x内部顺序对它们进行排序,type并计算有多少其他行的行n数高于x(列pos).
例如
df <- data.frame(type = c("a","a","a","b","b","b"),x=c(1,77,1,34,1,8))
# for type a row 3 has a higher x than row 1 and 2 so has a pos value of 2
Run Code Online (Sandbox Code Playgroud)
我可以这样做:
library(plyr)
df <- data.frame(type = c("a","a","a","b","b","b"),x=c(1,77,1,34,1,8))
df <- ddply(df,.(type), function(x) x[with(x, order(x)) ,])
df <- ddply(df,.(type), transform, pos = (seq_along(x)-1) )
type x pos
1 a 1 0
2 a 1 1
3 a 77 2
4 b …Run Code Online (Sandbox Code Playgroud) 我有一个用于绘制填充轮廓的自定义功能,这些功能主要基于Carey McGilliard和Bridget Ferris的工作(http://wiki.cbr.washington.edu/qerm/sites/qerm/images/1/16/Filled.contour3.R)和http://wiki.cbr.washington.edu/qerm/index.php/R/Contour_Plots.
该filled.contour3函数在R 2.15.3中完美运行,但在R 3.0.x中抛出错误
Error in .Internal(filledcontour(as.double(x), as.double(y), z, as.double(levels), :
there is no .Internal function 'filledcontour'
Run Code Online (Sandbox Code Playgroud)
你能帮我解决一下这个问题,或者解决这个问题,这样我才能使用filled.contour3()R 3.0.x中的这个功能.*我的大部分工作都取决于这个功能而且我在LInux上,因此在生产机器上改变R版本并不容易.很乐意提供赏金.*
要重现错误,请首先提供以下信息
filled.contour3 <-
function (x = seq(0, 1, length.out = nrow(z)),
y = seq(0, 1, length.out = ncol(z)), z, xlim = range(x, finite = TRUE),
ylim = range(y, finite = TRUE), zlim = range(z, finite = TRUE),
levels = pretty(zlim, nlevels), nlevels = 20, color.palette = cm.colors,
col = …Run Code Online (Sandbox Code Playgroud) 我有以下数据和代码:
mydf
grp categ condition value
1 A X P 2
2 B X P 5
3 A Y P 9
4 B Y P 6
5 A X Q 4
6 B X Q 5
7 A Y Q 8
8 B Y Q 2
>
>
mydf = structure(list(grp = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L), .Label = c("A", "B"), class = "factor"), categ = structure(c(1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("X", …Run Code Online (Sandbox Code Playgroud) 我试图计算数字样本("S")的平均平均偏差.我在使用"mad()"函数时得到的结果以及一次一步地进行平均偏差计算的结果是不同的.为什么?
s<- c(100,110,114,121,130,130,160)
Run Code Online (Sandbox Code Playgroud)
使用"mad()"函数,我得到:
> mad(s)
[1] 13.3434
Run Code Online (Sandbox Code Playgroud)
当分解公式并一次一步地执行相同的操作时,我得到:
> sum(abs(s-mean(s)))/length(s)
[1] 14.08163
Run Code Online (Sandbox Code Playgroud)
为什么这些结果有所不同?
我在输入配方时出错了吗?(这并不奇怪 - 我刚开始学习R).我的配方有什么问题?
或者是R用来计算与以下不同的平均平均偏差的公式(在维基百科上给出)
MAD =(((每个值减去样品的平均值))的绝对值之和除以(样品中的值的数量)?
(谢谢您的帮助!)
我有一些代码可以产生分层加权手段,我确信这在几个月前就有效了.但是,但我不确定目前的问题是什么.(我道歉 - 这一定是非常基本的东西):
dp=
structure(list(seqn = c(1L, 2L, 3L, 4L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 3L, 4L, 9L, 10L, 11L, 14L, 8L, 11L, 12L, 10L,
5L, 13L, 2L, 14L, 3L, 9L, 6L, 7L), sex = c(2L, 1L, 2L, 2L, 1L,
2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), bmi = c(22.8935608711259,
27.0944623781918, 40.4637162938634, 23.7649712675423, 15.3193372705538,
31.1280302540991, 21.4866354393239, 20.3200254374398, 32.331092513536,
25.3679771839413, …Run Code Online (Sandbox Code Playgroud) 我经常要将许多数字(数百)导出到一个文件中.目前我用
print('-dpsc2', outfile, '-append');
Run Code Online (Sandbox Code Playgroud)
我的代码使用了一个隐藏的数字,正在为每个新数字重用它.
现在,问题是我可以达到每秒8个数字的最大输出速度.在R中,您可以轻松地绘制每秒200个数字.有没有人有任何建议如何(大幅)加快MATLAB的出口能力?
一月
这是我的数据:
data <- structure(list(Indicator = structure(c(1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L, 1L, 2L, 2L,
2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L, 1L, 2L,
2L, 2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, …Run Code Online (Sandbox Code Playgroud) 我在大学教数学和编程(与R),我是一个良好和一致的符号的忠实粉丝.请看一下R中的以下简单向量运算:
> v1 <- c(1,2,3)
> v2 <- c(4,5,6)
> v1 %*% v2
[,1]
[1,] 32
> t(v1) %*% v2
[,1]
[1,] 32
> v1 %*% t(v2)
[,1] [,2] [,3]
[1,] 4 5 6
[2,] 8 10 12
[3,] 12 15 18
> t(v1) %*% t(v2)
Error in t(v1) %*% t(v2) : non-conformable arguments
> v1 + v2
[1] 5 7 9
> v1 + t(v2)
[,1] [,2] [,3]
[1,] 5 7 9
> t(v1) + t(v2)
[,1] [,2] [,3] …Run Code Online (Sandbox Code Playgroud) ItemID,Sentiment,SentimentSource,SentimentText
1,0,Sentiment140, ok thats it you win.
2,0,Sentiment140, i think mi bf is cheating on me!!! T_T
3,0,Sentiment140," I'm completely useless rt now. Funny, all I can do is twitter. "
Run Code Online (Sandbox Code Playgroud)
如何将这样的 csv 文件读入 R 中?
我正在使用一个包含1900列和大约280,000行的data.table.
目前,数据完全是"整数",但我希望它们明确地"数字",所以我可以稍后将它传递给bigcor()函数.显然,bigcor()只能处理"数字"而不是"整数".
我试过了:
full.bind <- full.bind[,sapply(full.bind, as.numeric), with=FALSE]
Run Code Online (Sandbox Code Playgroud)
不幸的是,我收到错误:
Error in `[.data.table`(full.bind, , sapply(full.bind, as.numeric), with = FALSE) :
j out of bounds
Run Code Online (Sandbox Code Playgroud)
所以,我尝试使用data.table set()函数,但是我得到了错误:
Error in set(full.bind, value = as.numeric(full.bind)) :
(list) object cannot be coerced to type 'double'
Run Code Online (Sandbox Code Playgroud)
我创建了一个简单的可重现的例子.请记住,实际列不是"a","b"或"c"; 它们是非常复杂的列名,因此不可能单独引用列.
dt <- data.table(a=1:10, b=1:10, c=1:10)
Run Code Online (Sandbox Code Playgroud)
所以,我最后的问题是:
1)为什么我的sapply技术不起作用?(什么是"j out of bounds"错误?)2)为什么set()技术不是?(为什么data.table不能被强制转换为数字?)3)bigcor()函数是否需要数字对象,还是存在其他问题?