Calculation at a specific value of a dataframe?

Asked

Viewed 1,264 times

-1

Possessing this data set:

   Índice                Produto  Classificação Comum Quilo
1       2          ABACAXI HAVAI       A GRAUDO  3,32   2,2
2       3          ABACAXI HAVAI        B MEDIO  2,81   1,8
3       4          ABACAXI HAVAI        C MIUDO  2,21   1,4
4       5            BANANA MACA              -   4,5     1
5       6        BANANA PRATA MG              -  3,13     1

I want to multiply some values of the common column by a constant, for example, the 3 values of pineapple I want to multiply by 100, thus staying:

   Índice                Produto  Classificação Comum Quilo
1       2          ABACAXI HAVAI       A GRAUDO   332   2,2
2       3          ABACAXI HAVAI        B MEDIO   281   1,8
3       4          ABACAXI HAVAI        C MIUDO   221   1,4
4       5            BANANA MACA              -   4,5     1
5       6        BANANA PRATA MG              -  3,13     1

How can I do it for R?

2 answers

2


My first step here is to replace the commas with dots. For example, the R does not consider 1.54 a number. This is a string for the program. Therefore, it is necessary to do this first step. The function gsub solves this problem in a row:

df$Comun <- as.numeric(gsub(",", "\\.", df$Comun))

Up there I’m asking for the R replace all commas with dots (which should be represented by \\. because they are special characters) in the column Comun of data frame df.

Now it is possible to do the desired multiplication. Just select the lines that matter in the data frame, multiply the corresponding column by 3 and replace in the original data frame:

df[df$Produto=="ABACAXI HAVAI", "Comun"]*100

In the above command I am choosing the lines with ABACAXI HAVAI in the product column Produto. Also, I needed to specify the column Comun to indicate where the numerical values that interested me were. With these values selected, just multiply them by 100 and the result will be ready.


I tried to solve this problem using package functions dplyr, but it was getting too complicated and I gave up.

  • If I want to add other products in multiplication just put one &? Ex: df[df$Product="PINEAPPLE HAWAII" & "BANANA MACA", "Comun"]*100

  • Marcus, another point, I want this new value after multiplication to replace the old value

  • The answer to the first question is no. You need to ask | (or) to choose lines with two or more products. The answer to the second question is df[df$Produto=="ABACAXI HAVAI", "Comun"]&#xA; <- df[df$Produto=="ABACAXI HAVAI", "Comun"]*100.

0

There are several ways to manipulate the values of a column of a data.frame. With the base functions of R, assuming your data.frame is called df, in your case just do:

df$Comum <- df$Comum*100

If you want to multiply only the line with "PINEAPPLE HAWAII":

df$Comum[df$Produto == "ABACAXI HAVAI"] <- df$Comum[df$Produto == "ABACAXI HAVAI"]*100

If you want to filter more lines use %in%:

   df$Comum[df$Produto %in% c("ABACAXI HAVAI","BANANA MACA")] <- df$Comum[df$Produto %in% c("ABACAXI HAVAI","BANANA MACA")]*100

Giving an example with the base mtcars that already comes in the R:

head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Let’s multiply the column mpg by 3:

mtcars$mpg <- mtcars$mpg*3
head(mtcars)
                  mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         63.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     63.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        68.4   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    64.2   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 56.1   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           54.3   6  225 105 2.76 3.460 20.22  1  0    3    1

Multiplying only with lines cyl == 6:

 mtcars$mpg[mtcars$cyl == 6] <- mtcars$mpg[mtcars$cyl == 6]*3

It is also possible to do the same thing with the function mutate() package dplyr:

library(dplyr)
mtcars <- mtcars %>% mutate(mpg = mpg*3)
  • My goal is to multiply a single or only some data.frame values, not the entire column

  • @Daniloimbimbo ok, just select the lines you want, I will include in the resposa

Browser other questions tagged

You are not signed in. Login or sign up in order to post.