以下数据用于进行比较分析。我使用apply()
和while()
编写了代码,尽管它按预期工作,但我还没有成功地进一步优化它。在较大的数据集中,当前运行时间超过几个小时。
以下是小示例数据集:
_1
A B C D
2 1 3 2.5
_2
P Q R S
3 2 4 5.5
数据
A B C D
1.0 0.5 1.3 1.5
1.5 1.2 5.5 3.5
1.1 0.5 1.3 1.5
1.5 1.2 5.5 3.5
1.5 1.2 5.5 3.5
1.1 0.5 1.3 1.5
1.5 1.2 5.5 3.5
1.0 0.5 1.3 1.5
代码
# Row counter
rowLine <<- 0
# Set current column to first one
columnLine <<- 1
# Preserve column header and dimensions for final data
finalData <- Data
# Find recursively
findThreshold <- function () {
if ( columnLine <= ncol(Data) ){
# Initialize row navigation to zero
rowLine <<- 1
# Navigate through rows
while (rowLine <= nrow(Data)){
# If outside threshold
if ( (Data[rowLine, columnLine] < data_1[columnLine]) |
(Data[rowLine, columnLine] > data_2[columnLine])){
finalData[rowLine, columnLine] <<- 1
} else {
finalData[rowLine, columnLine] <<- 0
}
# Increment row counter
rowLine <<- rowLine + 1
}
}
# Increment column counter
columnLine <<- columnLine + 1
}
# Apply
apply(Data, 2, function(x) findThreshold())
我也理解使用<<-
时使用loops
和apply()
进行递归分析时使用$(document).ready(function() {
var curUnit = "F";
//Backgrounds
var clearday = "url('https://static.pexels.com/photos/125457/pexels-photo-125457.jpeg')";
var clearnight = "url('https://static.pexels.com/photos/355465/pexels-photo-355465.jpeg')";
var rain = "url('https://static.pexels.com/photos/125510/pexels-photo-125510.jpeg')";
var snow = "url('https://static.pexels.com/photos/41088/pexels-photo-41088.jpeg')";
var sleet = "url('https://static.pexels.com/photos/27629/pexels-photo-27629.jpg')";
var wind = "url('https://static.pexels.com/photos/6041/nature-grain-moving-cereal.jpg')";
var fog = "url('https://static.pexels.com/photos/109037/pexels-photo-109037.jpeg')";
var cloudy = "url('https://static.pexels.com/photos/158163/clouds-cloudporn-weather-lookup-158163.jpeg')";
var partlycloudyday = "url('https://static.pexels.com/photos/152536/pexels-photo-152536.jpeg')";
var partlycloudynight = "url('https://static.pexels.com/photos/23985/pexels-photo-23985.jpg')";
//location API
var darksky = 'https://cors-anywhere.herokuapp.com/https://api.darksky.net/forecast/';
var apikey = "15f8bf5641489ec32f66662221933c14";
$("#myweather").load("window",function() {
$.ajax({
url: "https://geoip-db.com/jsonp",
jsonpCallback: "callback",
dataType: "jsonp",
success: function(loc) {
$("#location").load("window", function() {
$("#location").html(loc.city).fadeIn(1000);
});
//Weather Summary
$("#result").load("window", function() {
$.getJSON(darksky + apikey + "/" + loc.latitude + "," + loc.longitude, function(forecast){
$("#result").html(forecast.currently.summary).fadeIn(2000);
//Skycon
function addSkycon() {
var skycons = new Skycons({color: 'white'});
var skyconCanvas = document.getElementById("skycon");
skycons.add(skyconCanvas, forecast.currently.icon);
var iconsum = forecast.currently.icon
skycons.play();
$("#skycon").fadeIn(2000);
if(iconsum == "clear-day") {
document.body.style.backgroundImage = clearday;
}
else if(iconsum == "clear-night") {
document.body.style.backgroundImage = clearnight;
}
else if(iconsum == "rain") {
document.body.style.backgroundImage = rain;
}
else if(iconsum == "snow") {
document.body.style.backgroundImage = snow;
}
else if(iconsum == "sleet") {
document.body.style.backgroundImage = sleet;
}
else if(iconsum == "wind") {
document.body.style.backgroundImage = wind;
}
else if(iconsum == "fog") {
document.body.style.backgroundImage = fog;
}
else if(iconsum == "cloudy") {
document.body.style.backgroundImage = cloudy;
}
else if(iconsum == "partly-cloudy-day") {
document.body.style.backgroundImage = partlycloudyday;
}
else if(iconsum == "partly-cloudy-night") {
document.body.style.backgroundImage = partlycloudynight;
}
} addSkycon();
//Temperature
$("#corf").load("window", function() {
$("#corf").fadeIn(2000);
});
var ftemp = Math.round(forecast.currently.temperature);
var ctemp = Math.round((forecast.currently.temperature - 32) *
(5 / 9))
$("#temp").html(ftemp).fadeIn(2000);
$("#corf").on("click", function() {
$("#temp").fadeOut(function() {
if (curUnit == "F") {
$("#temp").html(Math.round(ctemp));
$("#degreeswitch").html("°c");
curUnit = "C";
}
else {
$("#temp").html(Math.round(ftemp));
$("#degreeswitch").html("°f");
curUnit = "F";
}
$("#temp").fadeIn();
});
});
});
});
}
});
});
});
是一个很大的问题。
请建议我如何进一步改进这种逻辑,谢谢。
答案 0 :(得分:3)
听起来像一个简单的Map
练习:
data.frame(Map(function(d,l,h) d < l | d > h, Data, data_1, data_2))
# A B C D
#1 TRUE TRUE TRUE TRUE
#2 TRUE FALSE TRUE FALSE
#3 TRUE TRUE TRUE TRUE
#4 TRUE FALSE TRUE FALSE
#5 TRUE FALSE TRUE FALSE
#6 TRUE TRUE TRUE TRUE
#7 TRUE FALSE TRUE FALSE
#8 TRUE TRUE TRUE TRUE
如果你想改为输入0/1,只需在as.integer
中包装逻辑比较:
data.frame(Map(function(d,l,h) as.integer(d < l | d > h), Data, data_1, data_2))
如果您的数据是matrix
个对象,则可以使用sweep
:
sweep(Data, 2, data_1, FUN=`<`) | sweep(Data, 2, data_2, FUN=`>`)
# A B C D
#[1,] TRUE TRUE TRUE TRUE
#[2,] TRUE FALSE TRUE FALSE
#[3,] TRUE TRUE TRUE TRUE
#[4,] TRUE FALSE TRUE FALSE
#[5,] TRUE FALSE TRUE FALSE
#[6,] TRUE TRUE TRUE TRUE
#[7,] TRUE FALSE TRUE FALSE
#[8,] TRUE TRUE TRUE TRUE