我有一些看起来像这样的数据:
Year Revenue Cost Rent
1 2016 3000 4 100
2 2017 4000 5 100
3 2018 5000 6 100
df <- data.frame(stringsAsFactors=FALSE,
Year = c(2016L, 2017L, 2018L),
Revenue = c(3000L, 4000L, 5000L),
Cost = c(4L, 5L, 6L),
Rent = c(100L, 100L, 100L)
)
我想将所有说的话除以Rent
的百分比:
library(dplyr)
df <- df %>% mutate_at(vars(Revenue:Rent), funs(. /Rent))
效果很好。
Year Revenue Cost Rent
1 2016 30 0.04 1
2 2017 40 0.05 1
3 2018 50 0.06 1
唯一的是:我已经失去了原来的专栏。
如何进行mutate_all
,以便有新的列,称为Revenue_percentage_of_rent
,Cost_percentage_of_rent
?
答案 0 :(得分:2)
在public class StageDrag extends Application {
@Override
public void start(Stage primaryStage) {
Pane root = new Pane();
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.addEventFilter(MouseEvent.MOUSE_RELEASED, e ->{System.out.println("released");});
//scene.addEventHandler(MouseEvent.MOUSE_RELEASED, e ->{System.out.println("released2");});
//root.addEventHandler(MouseEvent.MOUSE_RELEASED, e ->{System.out.println("released3");});
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
#include <stdio.h>
int main() {
int n;
printf("Enter width: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j || (i + j + 1) == n) {
printf("X");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
您可以使用void recursiveProblem(int num, int max) {
if (num <= (max / 2 + 1))
...
}
来做到这一点,但是它还会将import csv
import pandas as pd
file_name = ['sample_file1.csv', 'sample_file2.csv', 'sample_file3.csv']
content = []
for n in file_name:
with open(n) as f:
reader = csv.reader(f, delimiter=',')
content += list(reader)[1:] # If the first row contains headers
df = pd.DataFrame(content)
df.columns = ['Sensor ID' , 'Time Instant' , 'Measurement']
列除以mutate_at
,我想您不需要。
使用library(dplyr)
df %>% mutate_at(vars(Revenue:Rent), funs(percentage_of_rent = . /Rent))
的解决方法是
mutate_all
但是您在此处失去了Year
列。
答案 1 :(得分:2)
不赞成使用funs
,而推荐使用list
中的dplyr_0.8.0
,因此,选择是
library(dplyr)
df %>%
mutate_at(vars(Revenue:Rent), list(percentage_of_rent = ~ ./Rent))
# Year Revenue Cost Rent Revenue_percentage_of_rent Cost_percentage_of_rent Rent_percentage_of_rent
#1 2016 3000 4 100 30 0.04 1
#2 2017 4000 5 100 40 0.05 1
#3 2018 5000 6 100 50 0.06 1