-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_defined_function.R
45 lines (31 loc) · 950 Bytes
/
user_defined_function.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function_name <- function(parameter1, parameter2, ...) {
# Function body - code to be executed
# It may include statements, calculations, loops, etc.
# Return statement (optional)
return(result)
}
##define the user definrf functions
my_function <- function(arg1, arg2) {
result <- arg1 + arg2
return(result)
}
# usage of the function
result_value <- my_function(3, 5)
print(result_value)
library(dplyr)
complex_function <- function(data_frame) {
# Filter data_frame for specific condition
filtered_data <- data_frame %>%
filter(column_name > 10)
# Loop through each row and perform a calculation
result <- numeric(nrow(filtered_data))
for (i in 1:nrow(filtered_data)) {
result[i] <- filtered_data$column_name[i] * 2
}
# Return the final result
return(result)
}
# usage with a sample data frame
sample_data <- data.frame(column_name = c(5, 15, 8, 20))
final_result <- complex_function(sample_data)
print(final_result)