O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

Follow these steps to correct the data from your dataset.

  1. View some records of the dataset:
> View(crimeData)
  1. Add the column name state in the dataset:
> crimeData <- cbind(state = rownames(crimeData), crimeData)
  1. Gather all the variables between Murder and UrbanPop:
> crimeData.1 <- gather(crimeData,key = "crime_type",value = "arrest_estimate",Murder:UrbanPop) > crimeData.1
  1. Gather all the columns except the column state:
> crimeData.2 <- gather(crimeData,key = "crime_type",value = "arrest_estimate",-state) > crimeData.2
  1. Gather only the Murder and Assault columns:
> crimeData.3 <- gather(crimeData,key = "crime_type",value = "arrest_estimate",Murder, Assault) > crimeData.3
  1. Spread crimeData.2 to turn a pair of key ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required