Buenas, estoy dando mis primeros pasos en R y en la práctica de un curso que estoy haciendo, no me funciona la función "mutate". Dejo el código que es muy simple (principiante nivel 0) y espero que alguno me pueda ayudar con la duda.
data_mutate <- mutate(data, ValorItem = Total / Cantidad)
Gracias por tu respuesta. Tengo todas las librerias instaladas y ejecutadas. Ése sería el código completo. Pero no funciona y siempre me tira el mismo error.
data <- read.csv(file = "C:/Users/USUARIO/Desktop/COMPRAS.csv",
sep = ";",
encoding = "latin1") # the data file is located in Desktop?
# next to run this, you could see the data?
data # run this
class(data) # run this.
names(data) # For see the names of columns and write like show in the console.
# run this
data_mutate <- data %>%
mutate( ValorItem = Total / Cantidad)
I do not see that in your code. Please run my code exactly as it is written.
To format the output in your reply, put a line with three back ticks before and after the pasted output, like this:
```
the output of the code goes here
```
The result of your search() function does not include dplyr. That is why the mutate() function is not found by R. In the example below, I define DF then I run search() to show that dplyr is not loaded. When I try to use mutate, I get an error. Then I execute library(dplyr) and run search() again. Now package:dplyr is the second item returned by search(). I can then use mutate().
It is not necessary to run search() to load a package. I did it to illustrate the cause of your problem - you need to run the code library(dplyr)
DF <- data.frame(Total = c(8,5,7,6),
Cantidad = c(2,3,1,4))
search()
#> [1] ".GlobalEnv" "package:stats" "package:graphics"
#> [4] "package:grDevices" "package:utils" "package:datasets"
#> [7] "package:methods" "Autoloads" "tools:callr"
#> [10] "package:base"
DFmutate <- mutate(DF, ValorItem = Total/Cantidad)
#> Error in mutate(DF, ValorItem = Total/Cantidad): could not find function "mutate"
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
search()
#> [1] ".GlobalEnv" "package:dplyr" "package:stats"
#> [4] "package:graphics" "package:grDevices" "package:utils"
#> [7] "package:datasets" "package:methods" "Autoloads"
#> [10] "tools:callr" "package:base"
DFmutate <- mutate(DF, ValorItem = Total/Cantidad)
DFmutate
#> Total Cantidad ValorItem
#> 1 8 2 4.000000
#> 2 5 3 1.666667
#> 3 7 1 7.000000
#> 4 6 4 1.500000