user12081
user12081

Reputation: 157

Split column with tidyr with different lenghts

I want to separate a column with tidyr to extract the grade level. The Column looks like this:

School.Name
School A ES
SchoolB MS

The is no standard way the schools are named, so when I use separate

separate(DF, School.Name,c("School.Name","Number","Grade Level")

I get this

School.Name  Number   Grade Level
School           A         ES
SchoolB         MS         NA

Is there a way to tell tidyr to read from the right rather that from the left

Upvotes: 0

Views: 136

Answers (1)

xtluo
xtluo

Reputation: 2121

try ?separate:

separate(DF, School.name, c("School.Name","Number","Grade Level"), fill = "left")

Then you got result like :

  School.Name  Number Grade Level
1      school       A          ES
2        <NA> schoolB          MS

EDIT:

parameter fill controls when separated characters size doesn't match column size, optional warn, right, left.

  1. Case 1: separated characters size < column size

e.g.

"schoolB MS" to C("A", "B", "C"), fill = "left" : <NA> schoolB MS


"schoolB MS" to C("A", "B", "C"), fill = "right" : schoolB MS <NA>
  1. Case 2: separated characters size > column size

e.g.

"schoolB MS" to C("A"), fill = "warn" : schoolB #default drop extra from the right

Upvotes: 2

Related Questions