我有一个脚本,我正在使用ddply,如下例所示:
ddply(df, .(col),
function(x) data.frame(
col1=some_function(x$y),
col2=some_other_function(x$y)
)
)
Run Code Online (Sandbox Code Playgroud)
在ddply中,是否可以重用col1而无需再次调用整个函数?
例如:
ddply(df, .(col),
function(x) data.frame(
col1=some_function(x$y),
col2=some_other_function(x$y)
col3=col1*col2
)
)
Run Code Online (Sandbox Code Playgroud) 我用ggplot2在R中制作了一堆线图,我想把它们保存为jpegs.但是,我想让图表的分辨率更高或更高,这样如果你在查看它们时放大图形,它们看起来就不那么像素化了.
这是一段代码片段:
library("ggplot2")
p <- ggplot(df1)
p <- p +
geom_line(aes(time, ee_amt, colour="ee_amt"), size = 2) +
geom_point(aes(time, ee_amt, colour="ee_amt"), size = 2)
jpeg("G:\\Auto Parts\\sample.jpg")
print(p)
dev.off()
Run Code Online (Sandbox Code Playgroud) 给定数据帧
df1 <- data.frame(CustomerId=c(1:6),Product=c(rep("Toaster",3),rep("Radio",3)))
df2 <- data.frame(CustomerId=c(2,4,6),State=c(rep("Alabama",2),rep("Ohio",1)))
Run Code Online (Sandbox Code Playgroud)
存储在列表中
dflist <- c(df1,df2)
Run Code Online (Sandbox Code Playgroud)
如何在这些数据帧上运行sqldf查询(连接)?
尝试失败:
test <- sqldf("select a.CustomerId, a.Product, b.State from dflist[1] a
inner join dflist[2] b on b.id = a.id")
test <- sqldf("select a.CustomerId, a.Product, b.State from dflist$df1 a
inner join dflist$df2 b on b.CustomerId = a.CustomerId")
Run Code Online (Sandbox Code Playgroud) 我有两个不同长度的表,我需要将它们合并为两个常见的列(季节和客户端),并在没有共同元素的情况下用NA填充单元格.下面我展示了我需要的两张原始表和最终表中的一小部分.我尝试了许多没有成功的事情.
season client.ID qtty
1998 13 30
1999 13 30
2000 13 29
1998 28 18
1999 28 18
2000 28 18
1998 35 21
1999 35 21
2000 35 21
season client.ID vessel.ID overLength
1998 28 29 17.1
1998 28 1809 4.26
1998 28 2215 9.45
1998 28 4173 5.8
1998 28 8151 4.5
1999 28 29 17.1
1999 28 1809 4.26
1999 28 2215 9.45
1999 28 4173 5.8
1999 28 8151 4.5
2000 28 29 17.1
2000 …Run Code Online (Sandbox Code Playgroud) 我正在尝试将二进制文件读入R,但是该文件具有用二进制代码编写的数据行。因此,它没有属于一列的完整数据集,而是存储为数据行。这是我的数据:
字节1-4:int ID字节5:char响应字符字节6-9:int Resp Dollars字节10:char类型char
任何人都可以帮助我弄清楚如何将此文件读入R?
嗨,大家好,
这是到目前为止我测试过的代码。我尝试了几件事,但收效甚微。不幸的是,我不能将任何数据发布在公共网站上,抱歉。我是R的新手,因此在如何改进代码方面需要一些帮助。提前致谢。
> binfile = file("File Location", "rb")
> IDvals = readBin(binfile, integer(), size=4, endian = "little")
> Responsevals = readBin(binfile, character (), size = 5)
> ResponseDollarsvals = readBin (binfile, integer (), size = 9, endian= "little")
Error in readBin(binfile, integer(), size = 9, endian = "little") :
size 9 is unknown on this machine
> Typevals = readBin (binfile, character (), size=4)
> binfile1= cbind(IDvals, Responsevals, ResponseDollarsvals, Typevals)
> dimnames(binfile1)[[2]]
[1] "IDvals" …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的时间序列数据集,包括单个变量的年平均值("AVERAGE").我想调查时间序列的"趋势"组成部分的变化率(一阶导数)和加速度(二阶导数)以及相关的标准误差.我使用MGCV的GAM和PREDICT功能获得了"趋势",简单如下:
A <- gam(AVERAGE ~ s(YEAR), data=DF, na.action=na.omit)
B <- predict(A, type="response", se.fit=TRUE)
Run Code Online (Sandbox Code Playgroud)
我通过2种不同的方法确定了导数,应用了高DoF立方光滑样条,并通过第一和第二差异(轻微平滑)和自举来近似误差,两者都产生了可比较的结果.
我注意到"gam.fit3"功能有助于确定最多二阶导数但不直接调用.我还注意到使用类型为"lpmatrix"的"predict.gam"有助于平滑的衍生.我想直接使用"GAM"函数计算第一和第二衍生物,但是不足以计算或提取这些衍生物.我尝试在"Predict.gam"帮助页面的末尾重新配置Wood的示例,但是没有成功.让我走向正确方向的任何帮助都会非常棒.谢谢菲尔.
我试图让箱形图从具有最低平均值的因子发展到具有最高平均值的因子.这是一个简单的例子:
a = rnorm(10,mean=3,sd=4)
b = rnorm(10,mean=-1,sd=2)
c = rnorm(10,mean=5,sd=6)
d = rnorm(10,mean=-3,sd=1)
e = rnorm(10,mean=0,sd=.5)
labs = c(rep("a",10),rep("b",10),rep("c",10),rep("d",10),rep("e",10))
mean = c(rep(mean(a),10),rep(mean(b),10),rep(mean(c),10),rep(mean(d),10),rep(mean(e),10))
data = c(a,b,c,d,e)
df = data.frame(labs,data,mean)
df = df[order(df$mean),]
boxplot(data~labs,data=df)
#They are not ordered
df$labs = ordered(df$labs, levels=levels(df$labs))
boxplot(data~labs,data=df)
#It doesn't work
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到左边最小的因子,随着我向右走?这有几个主题,但他们的方法对我不起作用.(也许是因为我的数据格式?)
奖励积分帮助我将x轴上的字母旋转180度.
提前致谢!
我有一个小问题,我不确定原因,但以下针织代码:
<<toto, echo=FALSE, comment=NA, results='asis'>>=
#Data
a<- c(1:100)
b<- c(100:200)
#Plot1
plot(a)
#Plot2
plot(b)
@
Run Code Online (Sandbox Code Playgroud)
生成这个LaTeX代码:
\includegraphics[width=\maxwidth]{figure/toto1}
(empty line here)
\includegraphics[width=\maxwidth]{figure/toto2}
(empty line here)
Run Code Online (Sandbox Code Playgroud)
如何删除此空行?
我有一堆带有标点符号的字符串,我想将其转换为空格:
"This is a string. In addition, this is a string (with one more)."
Run Code Online (Sandbox Code Playgroud)
会成为:
"This is a string In addition this is a string with one more "
Run Code Online (Sandbox Code Playgroud)
我可以通过stringrpackage(str_replace_all())一次手动执行此操作(,/./!/(/)/等),但我很好奇是否有更快的方式我假设使用正则表达式.
有什么建议?
我有两个相同长度的对称矩阵(一个包含相关系数,另一个包含p值).
我试图建立一个矩阵,使得upper.tri包含相关系数,而lower.tri包含相关的p值.