Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何计算列表中数字之间的差异?_R_Loops_Vector - Fatal编程技术网

R 如何计算列表中数字之间的差异?

R 如何计算列表中数字之间的差异?,r,loops,vector,R,Loops,Vector,我有一个数字列表: head(x[[1]]) [1] 10990.16 10959.95 10942.02 10935.00 10867.84 10863.00 我想计算所有相邻数之间的差(n-(n-1))。在上述情况下,输出将为30.21、17.93、7.02 有人对一次计算所有这些值有什么建议吗 谢谢。试试看 x <- c(10990.16, 10959.95, 10942.02, 10935.00, 10867.84, 10863) abs(diff(x)) #[1] 30.21

我有一个数字列表:

head(x[[1]])
[1] 10990.16 10959.95 10942.02 10935.00 10867.84 10863.00
我想计算所有相邻数之间的差(n-(n-1))。在上述情况下,输出将为30.21、17.93、7.02

有人对一次计算所有这些值有什么建议吗

谢谢。

试试看

x <- c(10990.16, 10959.95, 10942.02, 10935.00, 10867.84, 10863)
abs(diff(x))
#[1] 30.21 17.93  7.02 67.16  4.84

试一试

试一试

试一试


就这么简单。谢谢。@user1038055是的,是的。@David Arenburg看起来是,(我可能错了):-)就这么简单。谢谢。@user1038055是的,是的。@David Arenburg看起来是,(我可能错了):-)就这么简单。谢谢。@user1038055是的,是的。@David Arenburg看起来是,(我可能错了):-)就这么简单。谢谢。@user1038055是的,是的。@David Arenburg看起来是的,(我可能错了):-)
abs(x[-1]-x[-length(x)])
#[1] 30.21 17.93  7.02 67.16  4.84
 head(x,-1)- tail(x,-1)