我已经能够找到每列连续两行之间的差异,但现在我需要将其除以前一行。
Date FXDL.AED.USD FXDL.ALL.USD
03/01/2005 3.6726 92.1
04/01/2005 3.6726 92.1
05/01/2005 3.6725 94.5
06/01/2005 3.6726 95.1
07/01/2005 3.6725 95.3
例如,每列的(第2行 - 第1行)/第1行(第3行 - 第2行)/第2行等之间的差异。
我使用diff_fxdl_weekdays <- sapply(fxdl_weekdays[,sapply(fxdl_weekdays, is.numeric)], diff)
来找出差异然后我尝试将其除以-nrow
,但这不起作用。
答案 0 :(得分:4)
一个想法是使用dplyr
包和lag
函数,
library(dplyr)
#define the function
fun1 <- function(x){ (x - lag(x))/lag(x)}
mutate_if(df, is.numeric, fun1)
# Date FXDL.AED.USD FXDL.ALL.USD
#1 03/01/2005 NA NA
#2 04/01/2005 0.000000e+00 0.000000000
#3 05/01/2005 -2.722867e-05 0.026058632
#4 06/01/2005 2.722941e-05 0.006349206
#5 07/01/2005 -2.722867e-05 0.002103049
如果您还想保留原始列,那么只需
mutate_if(df, is.numeric, funs('diff' = fun1))
# Date FXDL.AED.USD FXDL.ALL.USD FXDL.AED.USD_diff FXDL.ALL.USD_diff
#1 03/01/2005 3.6726 92.1 NA NA
#2 04/01/2005 3.6726 92.1 0.000000e+00 0.000000000
#3 05/01/2005 3.6725 94.5 -2.722867e-05 0.026058632
#4 06/01/2005 3.6726 95.1 2.722941e-05 0.006349206
#5 07/01/2005 3.6725 95.3 -2.722867e-05 0.002103049
答案 1 :(得分:2)
创建一个函数:
sapply(d[,sapply(d, is.numeric)], f)
将其应用于数字列:
# replace the numeric columns with the new ones:
d[, sapply(d, is.numeric)] <- sapply(d[,sapply(d, is.numeric)], f)
# add new colums to the dataframe:
d[, paste0(names(d)[sapply(d, is.numeric)],'_diff')] <- sapply(d[,sapply(d, is.numeric)], f)
如果你想将它添加到数据帧:
> d
Date FXDL.AED.USD FXDL.ALL.USD FXDL.AED.USD_diff FXDL.ALL.USD_diff
1 2005-01-03 3.6726 92.1 NA NA
2 2005-01-04 3.6726 92.1 0.000000e+00 0.000000000
3 2005-01-05 3.6725 94.5 -2.722867e-05 0.026058632
4 2005-01-06 3.6726 95.1 2.722941e-05 0.006349206
5 2005-01-07 3.6725 95.3 -2.722867e-05 0.002103049
后者的结果:
getData(1);
function getData(id) {
//Emty div
$("#surah-wrapper").empty();
$.ajaxSetup({
cache: true,
jsonpCallback: 'quranData'
}); // define ajax setup
// Quran Text Type quran-uthmani | quran-simple | quran-simple-clean | quran-wordbyword
$.getJSON("http://api.globalquran.com/surah/" + id + "/quran-uthmani?key=api_key&jsoncallback=?", {
format: "jsonp"
}, function(data) {
if (id > 1) {
$("<span class='qspan qspan-bsm'>").html("بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ").appendTo("#surah-wrapper");
}
$.each(data.quran, function(i, by) {
$.each(by, function(verseNo, line) {
//$("<p>").html('('+ line.surah+':'+line.ayah+') '+line.verse).appendTo("#surah-wrapper");
$("<span class='qspan' id='" + verseNo + "'>").html(line.verse + '<span class="qspan-ayahno">(' + line.surah + ':' + line.ayah + ')</span>').appendTo("#surah-wrapper");
});
});
});
}
//Play Script & highlight script
var audioIndex = 0;
var countSpan = 0;
countSpan = $('#surah-wrapper').children().length;
var surahNo = 1;
var strCat = "http://download.quranicaudio.com/verses/Sudais/mp3/001001.mp3,http://download.quranicaudio.com/verses/Sudais/mp3/001002.mp3,http://download.quranicaudio.com/verses/Sudais/mp3/001003.mp3,http://download.quranicaudio.com/verses/Sudais/mp3/001004.mp3,http://download.quranicaudio.com/verses/Sudais/mp3/001005.mp3,http://download.quranicaudio.com/verses/Sudais/mp3/001006.mp3,http://download.quranicaudio.com/verses/Sudais/mp3/001007.mp3";
setPlayer();
$('.customSurah').change(function() {
$('.play-btn').css('display', 'none');
$aud.pause();
surahNo = $('#surah option:selected').val();
setTimeout(function() {
countSpan = $('#surah-wrapper').children().length;
var i = 0;
strCat = '';
for (i = 0; i <= countSpan; i++) {
if (i == 0) {
strCat = "http://download.quranicaudio.com/verses/Sudais/mp3/001001.mp3,";
i += 1
}
if (i == countSpan) {
if (surahNo == 1) {
} else {
if (i < 10) {
strCat += "http://download.quranicaudio.com/verses/Sudais/mp3/00" + surahNo + "00" + i + ".mp3,";
}
}
} else {
if (i < 10) {
strCat += "http://download.quranicaudio.com/verses/Sudais/mp3/00" + surahNo + "00" + i + ".mp3,";
}
}
}
if (surahNo == 1) {
strCat = null;
strCat = "http://download.quranicaudio.com/verses/Sudais/mp3/001001.mp3,http://download.quranicaudio.com/verses/Sudais/mp3/001002.mp3,http://download.quranicaudio.com/verses/Sudais/mp3/001003.mp3,http://download.quranicaudio.com/verses/Sudais/mp3/001004.mp3,http://download.quranicaudio.com/verses/Sudais/mp3/001005.mp3,http://download.quranicaudio.com/verses/Sudais/mp3/001006.mp3,http://download.quranicaudio.com/verses/Sudais/mp3/001007.mp3";
}
setPlayer();
$('.play-btn').css('display', 'block');
}, 3000);
});
function setPlayer() {
//reset values
audioIndex = 0;
countSpan = 0;
countSpan = $('#surah-wrapper').children().length;
strCat = strCat.trim();
var audioTracks = strCat;
var audioAddress = audioTracks.split(',');
var playing = false;
$(function() {
$aud = $("#myAudio")[0];
$btn = $(".play-btn");
function setAudio(index) {
$("#surah-wrapper > .qspan").removeClass("qplaying");
$aud.preload = 'auto';
$aud.src = audioAddress[index];
}
setAudio(audioIndex);
$btn.click(function() {
if (playing) {
playing = false;
$aud.pause();
} else
$aud.play();
});
$aud.onended = function() {
if (audioIndex < audioAddress.length - 1) {
audioIndex++;
setAudio(audioIndex);
$aud.play();
} else {
audioIndex = 0;
setAudio(audioIndex);
playing = false;
$btn.text("Play");
}
};
$aud.onpause = function() {
if (!playing) $btn.text("Play");
$(".play-btn").css("background-image", "url(https://cdn0.iconfinder.com/data/icons/cosmo-player/40/button_play_1-64.png)");
};
$aud.onplay = function() {
$btn.text("Pause");
$(".play-btn").css("background-image", "url(https://cdn0.iconfinder.com/data/icons/cosmo-player/40/button_pause_1-64.png)");
playing = true;
$("#surah-wrapper > .qspan:nth-child(" + (audioIndex + 1) + ")").addClass("qplaying");
var wHeight = $(window).height();
var wHalfHeight = wHeight;
var x = $(".qplaying").offset();
var curentSpanPosition = x.top;
wHalfHeight = wHalfHeight / 2;
if (curentSpanPosition > wHalfHeight) {
$('html, body').animate({
scrollTop: curentSpanPosition - 50
}, 1000);
}
};
});
}