Calculating a specific value in a dataframe?

-1

Possessing this dataset:

   Í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:

   Í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 this by R?

    
asked by anonymous 09.03.2017 / 20:59

2 answers

1

My first step here is to replace commas with periods. For example,% w / o% does not consider a number as 1.54. This is a string for the program. Therefore, it is necessary to take this first step. The R function solves this problem on a line:

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

Here I am asking you to gsub replace all the commas by points (which must be represented by R because they are special characters) in column \. of data frame Comun .

Now you can make the multiplication you want. Just select the lines that interest in the data frame, multiply the corresponding column by 3 and replace in the original frame date:

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

In the above command I am choosing rows with df in the column product ABACAXI HAVAI . Also, I had to specify the Produto column to indicate where the numerical values were that interested me. With these values selected, simply multiplying them by 100 and the result will be ready.

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

    
11.03.2017 / 00:18
0

There are several ways to manipulate the values of a column in 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 just the line with "ABACAXI HAVAI":

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

If you want to filter more rows 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 mpg column 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

You can also do the same thing with the mutate() function of the dplyr package:

library(dplyr)
mtcars <- mtcars %>% mutate(mpg = mpg*3)
    
10.03.2017 / 23:52