有没有更好的方法将循环中生成的数据存储为data.frame(但仍使用循环)?
脚本:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
GridPane root = new GridPane();
RowConstraints rc = new RowConstraints();
rc.setVgrow(Priority.ALWAYS);
root.getRowConstraints().add(rc);
root.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
HBox hBox = new HBox();
hBox.setBackground(new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, Insets.EMPTY)));
hBox.setMinHeight(0);
ImageView imageView = new ImageView("http://www.calgary.ca/CA/city-manager/scripts/about-us/webparts/images/ourHistory_retina.jpg");
imageView.fitHeightProperty().bind(hBox.heightProperty());
imageView.setPreserveRatio(true);
hBox.getChildren().add(imageView);
root.add(hBox, 0, 0);
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:1)
如果你真的想使用一个循环,猜猜这个有效
Results <- data.frame()
Mystep <- 2
for(i in seq(0, 10, by = Mystep )){
xy = c(i*10, i/10)
Results = rbind(Results, xy)
}
names(Results) = c("X", "Y")
> Results
X Y
1 0 0.0
2 20 0.2
3 40 0.4
4 60 0.6
5 80 0.8
6 100 1.0
答案 1 :(得分:0)
使用循环不能有效地使用R.矢量化解决方案是正确的答案。但由于这已不在考虑范围之内,我认为双循环解决方案并不符合要求:
data.frame(
sapply(seq(0, 10, by = Mystep), `*`, 10)),
sapply(seq(0, 10, by = Mystep), `/`, 10))
)
或(清洁)
itr <- seq(0, 10, by = Mystep)
data.frame(sapply(itr, `*`, 10)), sapply(itr, `/`, 10)))
最后是一个for循环:
output <- data.frame(numeric(length(itr)), numeric(length(itr)))
for(i in itr) {
output[i, 1] <- itr*10
output[i, 2] <- itr/10
}