Shadow

Tag: R

Renaming Columns with R

Renaming Columns with R

Recent Posts, Data Science Using R
Often data you’re working with has abstract column names, such as (x1, x2, x3…). Typically, the first step I take when renaming columns with r is opening my web browser.  For some reason no matter the amount of times doing this it’s just one of those things. (Hoping that writing about it will change that) The dataset cars is data from the 1920s on "Speed and Stopping Distances of Cars". There is only 2 columns shown below. colnames(datasets::cars) [1] "speed" "dist" If we wanted to rename the column "dist" to make it easier to know what the data is/means we can do so in a few different ways. Using dplyr: cars %>% rename("Stopping Distance (ft)" = dist) %>% colnames() [1] "speed" "Stopping Distance (ft)" cars %>% rename("Stopping Di
Manipulating Data Frames in R

Manipulating Data Frames in R

Data Science Using R, Data Science
Learn To Manipulate Data Frames Using The “mtcars” Dataset Below is an introduction to programming with r, all code in this exercise is only using base r and no other libraries are needed. Task 1: Create a new column to find Displacement per Cylinder  Create a new variable (DisplacementPerCylinder), to calculate the total displacement per cylinder in cubic inches for each vehicle from the mtcars dataset. # "str" allows you to display the internal structure of an R object str(mtcars) ## 'data.frame': 32 obs. of 11 variables: ## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... ## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ... ## $ disp: num 160 160 108 258 360 ... ## $ hp : num 110 110 93 110 175 105 245 62 95 123 ... ## $ drat: num 3.9 3.9 3.85 3.08 3