Am new to R and I need to create a function called "salary_increment" that takes a dataframe and an increase percentage as arguments. The function must calculate the new value after applying the percentage increase to the "salary" variable in each row of the dataframe and return the result as a new dataframe.
Do you have any code written? Can you be more specific about what you are having trouble with and what parts you know how to do?
This is my code so far. Am having trouble creating the function to perform the specific task.
datos <- read.csv(file.choose(), header = TRUE)
str(datos)
summary(datos)
datos.filtrados <- filter(datos,Edad >= 30)
media.salario <- mean(datos.filtrados$Salario)
print(paste("La media de salario es",media.salario))
rango.intercuartilico <- IQR(datos$Edad)
print(paste("El rango intercuartilico de la edad es",rango.intercuartilico))
Here is an example of a function that takes a file name and number, reads the file and uses the number to calculate a value. For your problem, you need to change the value of a column in the data frame and return the data frame. Do you know how to change the value of a column?
salary_increment <- function(Nombre, mult) {
datos <- read.csv(Nombre)
media.salario <- mean(datos$Salario)
media.mult <- media.salario * mult
return(media.mult)
}
FileName <- file.choose()
Valor <- salary_increment(Nombre = FileName, mult = 2)
print(Valor)
library(charlatan)
# Set seed for reproducibility
set.seed(42)
# Create a fake data frame
fake <- data.frame(
name = ch_name(10),
position = ch_job(10),
current_salary = sample(75000:200000, 10)
)
fake
#> name position current_salary
#> 1 Nestor Hammes PhD Horticulturist, amenity 189609
#> 2 Rosalee Gaylord Occupational hygienist 86223
#> 3 Ezzard Nikolaus MD Information officer 108712
#> 4 Dr. Iridian Hegmann Higher education careers adviser 139409
#> 5 Orlo Sauer Curator 177661
#> 6 Dixon Murazik-Dibbert Trading standards officer 152953
#> 7 Denese Farrell Librarian, academic 194810
#> 8 Sage Tillman Engineer, agricultural 156300
#> 9 Annmarie Ondricka MD Retail manager 149742
#> 10 Linwood Harris English as a second language teacher 171953
# Define the function to multiply a column by a factor
bump <- function(data, column_name, factor) {
data[[column_name]] * factor
}
# Apply the function to current_salary column
fake$new_salary <- bump(fake, "current_salary", 1.08)
# View the updated data frame
fake
#> name position current_salary
#> 1 Nestor Hammes PhD Horticulturist, amenity 189609
#> 2 Rosalee Gaylord Occupational hygienist 86223
#> 3 Ezzard Nikolaus MD Information officer 108712
#> 4 Dr. Iridian Hegmann Higher education careers adviser 139409
#> 5 Orlo Sauer Curator 177661
#> 6 Dixon Murazik-Dibbert Trading standards officer 152953
#> 7 Denese Farrell Librarian, academic 194810
#> 8 Sage Tillman Engineer, agricultural 156300
#> 9 Annmarie Ondricka MD Retail manager 149742
#> 10 Linwood Harris English as a second language teacher 171953
#> new_salary
#> 1 204777.72
#> 2 93120.84
#> 3 117408.96
#> 4 150561.72
#> 5 191873.88
#> 6 165189.24
#> 7 210394.80
#> 8 168804.00
#> 9 161721.36
#> 10 185709.24
Created on 2024-01-06 with reprex v2.0.2
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.