使用这样的示例数据:
example=data.frame(x=c(1,2,3,4,5,6,7,8), y=c(1,2,3,4,5,6,7,8), z=c(1,2,3,4,5,6,7,8))
Run Code Online (Sandbox Code Playgroud)
看起来像这样:
x y z
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
8 8 8 8
Run Code Online (Sandbox Code Playgroud)
我想将z列中的所有值向上移动两行,而其余的数据帧保持不变.结果应如下所示:
x y z
1 1 1 3
2 2 2 4
3 3 3 5
4 4 4 6
5 5 5 7
6 6 6 8
7 7 7 NA
8 8 8 NA
Run Code Online (Sandbox Code Playgroud)
我只找到了将列的值向下移动或移动整个数据帧的方法.
有任何想法吗?谢谢!
我有以下情节:

我想更改 x 轴标签,以便我使用“May”而不是“Mai”(首选),或者如果失败,例如“14-05-21 18:00”,其中包含完整日期和小时给出。
我用于绘图的代码是:
library(ggplot2)
library(grid)
ggplot(day5, aes(datetime)) +
geom_line(aes(y = ET.Lys, colour = "ET.Lys")) +
geom_line(aes(y = ET.PM, colour = "ET.PM"))+
scale_y_continuous(breaks=c(0,0.2, 0.4, 0.6, 0.8, 1, 1.2), limits=c(0,1.2)) +
xlab("\n date and time") +
ylab("ET [mm/h]\n") +
theme_bw(25)+
theme(legend.title=element_blank(),
legend.justification = c(1, 1), legend.position = c(1,1),
legend.background=element_rect(color = "grey",fill = "white", size = 0.1, linetype = "solid"),
legend.key = element_blank(),
legend.text=element_text(size=20))+
geom_line(aes(y = ET.Lys, colour = "ET.Lys"), size=1)+
geom_line(aes(y = ET.PM, colour = "ET.PM"), size=1)+
theme(plot.margin=unit(c(0.5,2,0.5,0.5), "cm")) …Run Code Online (Sandbox Code Playgroud) 我正在数据框上应用 na.approx,如果 NA 恰好位于数据库的第一行或最后一行,则该方法将不起作用。
如何编写执行以下操作的函数:“当数据帧第一行的任何值为 NA 时,删除第一行”
数据框示例:
x1=x2=c(1,2,3,4,5,6,7,8,9,10,11,12)
x3=x4=c(NA,NA,3,4,5,6,NA,NA,NA,NA,11,12)
df=data.frame(x1,x2,x3,x4)
Run Code Online (Sandbox Code Playgroud)
此示例数据框的结果应如下所示:
result=df[-1:-2,]
Run Code Online (Sandbox Code Playgroud)
我当前的尝试看起来都类似于:
replace_na=function(df){
while(anyNA(df[1,])=TRUE){
df=df[-1,],
return(df)
}
#this is where I would apply the na.approx function to the data frame
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,谢谢!
我有一个具有两个值的数据集,date如下所示:
date x y
1 2013-05-01 1 2
2 2013-05-02 2 2
3 2013-05-03 3 2
Run Code Online (Sandbox Code Playgroud)
date是as.Date使用包的格式lubridate。
现在,我想拥有mean两个值中的一个,但要在一定的时间范围内使用的值x。
我尝试了以下方法:
mean=(x+y)/2
newdata=ifelse((data$date < 2013-10-01 | date$date > 2014-04-09), mean, x)
Run Code Online (Sandbox Code Playgroud)
但是如果只是将mean所有日期都设为。
在日期中是否可以使用比关系大/小的关系?关于如何进行这项工作的任何建议?
提前致谢
r ×4
dataframe ×2
as.date ×1
date ×1
datetime ×1
function ×1
ggplot2 ×1
if-statement ×1
na ×1
while-loop ×1