Error en función "mutate"

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.

Transformación de Datos

data <- read.csv(file = "C:/Users/USUARIO/Desktop/COMPRAS.csv",
sep = ";",
encoding = "latin1")

mutate

data_mutate <- mutate(data, ValorItem = Total / Cantidad)

Ése es el código que cargo en la consola y cuando le doy a correr para aplicar "mutate", me figura:

Error in mutate(data, ValorItem = Total/Cantidad) :
no se pudo encontrar la función "mutate"

Espero que alguno me pueda solucionar el problema, gracias!

mutate is a function in the package dplyr. You need to run library(dplyr) before using your code.

library(dplyr)
data <- read.csv(file = "C:/Users/USUARIO/Desktop/COMPRAS.csv",
sep = ";",
encoding = "latin1")

data_mutate <- mutate(data, ValorItem = Total / Cantidad)

If you have not installed dplyr, you need to do that first of all

install.packages("dplyr")

It is only necessary to run install.packages() once.
The library() command must be run once per R session to load the package.

1 Like

library(magrittr)
library(tidyverse)
library(dplyr)
library(ggplot2)

Transformación de Datos

data <- read.csv(file = "C:/Users/USUARIO/Desktop/COMPRAS.csv",
sep = ";",
encoding = "latin1")

mutate

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.

Hi @Julianllopis ,

Check the object data, and the correct path.

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.

Hola @M_AcostaCH ,

el archivo me lo abre, y si, está en el escritorio. La primer parte funciona bien y se abre el archivo. Lo que no funciona es el "mutate"!

Ok. Verify the names of the columns

names(data) # For see the names of columns and write like show in the console.
# run this 
data_mutate <- data %>%
  mutate( ValorItem = Total / Cantidad)

@Julianllopis
With this simple code

DF <- data.frame(Total = c(8,5,7,6),
                 Cantidad = c(2,3,1,4))
library(dplyr)
DFmutate <- mutate(DF, ValorItem = Total/Cantidad)
DFmutate

I get the following output

DF <- data.frame(Total = c(8,5,7,6),
                 Cantidad = c(2,3,1,4))
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
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

Created on 2023-11-14 with reprex v2.0.2

Please run my code and show all of the output.

DF <- data.frame(Total = c(8,5,7,6),

  •              Cantidad = c(2,3,1,4))
    

View(DF)
DFmutate <- mutate(DF, ValorItem = Total/Cantidad)
Error in mutate(DF, ValorItem = Total/Cantidad) :
no se pudo encontrar la función "mutate"

Esos son los resultados, me dice lo mismo. Copie el mismo código que me mandaste !

Immediately after

DF <- data.frame(Total = c(8,5,7,6),
                 Cantidad = c(2,3,1,4))

I have the line

library(dplyr)

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
```

DF <- data.frame(Total = c(8,5,7,6),
Cantidad = c(2,3,1,4))
library(dplyr)
DFmutate <- mutate(DF, ValorItem = Total/Cantidad)
DFmutate

Ésto escribí en la consola.

Resultado:

DF <- data.frame(Total = c(8,5,7,6),

  •              Cantidad = c(2,3,1,4))
    

DFmutate <- mutate(DF, ValorItem = Total/Cantidad)
Error in mutate(DF, ValorItem = Total/Cantidad) :
no se pudo encontrar la función "mutate"

Please run the search() function and post the output. Here is the result on my computer

 search()
 [1] ".GlobalEnv"        "tools:rstudio"     "package:stats"    
 [4] "package:graphics"  "package:grDevices" "package:utils"    
 [7] "package:datasets"  "package:methods"   "Autoloads"        
[10] "package:base"  

Hola, perdón la demora de la respuesta, me deja los mismos resultados:

search()
[1] ".GlobalEnv" "tools:rstudio" "package:stats"
[4] "package:graphics" "package:grDevices" "package:utils"
[7] "package:datasets" "package:methods" "Autoloads"
[10] "package:base"

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

Created on 2023-11-18 with reprex v2.0.2

This topic was automatically closed 42 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.