Importing column names with row names

This is an elementary question but I'm stumped.

I want to import data like this with column names:

Name mfr type calories protein fat sodium fiber carbo sugars shelf vitamins serving_Size cups
100%_Bran N C 70 4 1 130 10 5 6 3 280 25 1 0.33
100%_Natural_Bran Q C 120 3 5 15 2 8 8 3 135 0 1 -1
All-Bran K C 70 4 1 260 9 7 5 3 320 25 1 0.33
All-Bran_with_Extra_Fiber K C 50 4 0 140 14 8 0 3 330 25 1 0.5
Almond_Delight R C 110 2 2 200 1 14 8 3 -1 25 1 0.75
Apple_Cinnamon_Cheerios G C 110 2 2 180 1.5 10.5 10 1 70 25 1 0.75
Apple_Jacks K C 110 2 0 125 1 11 14 2 30 25 1 1
Basic_4 G C 130 3 2 210 2 18 8 3 100 25 1.33 0.75
Bran_Chex R C 90 2 1 200 4 15 6 1 125 25 1 0.67

The problem is that when imported, RStudio puts the Name label over the mfr column, the mfr column over the type column, and so on. I tried removing the Name label and RStudio reports an error because there are not enough column names.

How do I import this into RStudio with proper column names?

R is interpreting the first column as a row name because there is one less element in the first row, the headers, than in all the others. If you add one more name after cups, the data will import as you want.

There are two ways to fix this:

  1. At the source by changing the first line from
,Name,mfr,type,calories,protein,fat,sodium,fiber,carbo,sugars,shelf,vitamins,serving_Size,cups

if it's a csv file, by removing the first comma on that line.

  1. You don't always have control over the source, however, so here's another way, using the {utils} package
withrows <- read.csv("https://raw.githubusercontent.com/shifteight/R-lang/master/ISLR/College.csv", header = TRUE, row.names = 1)
head(withrows)
#>                              Private Apps Accept Enroll Top10perc Top25perc
#> Abilene Christian University     Yes 1660   1232    721        23        52
#> Adelphi University               Yes 2186   1924    512        16        29
#> Adrian College                   Yes 1428   1097    336        22        50
#> Agnes Scott College              Yes  417    349    137        60        89
#> Alaska Pacific University        Yes  193    146     55        16        44
#> Albertson College                Yes  587    479    158        38        62
#>                              F.Undergrad P.Undergrad Outstate Room.Board Books
#> Abilene Christian University        2885         537     7440       3300   450
#> Adelphi University                  2683        1227    12280       6450   750
#> Adrian College                      1036          99    11250       3750   400
#> Agnes Scott College                  510          63    12960       5450   450
#> Alaska Pacific University            249         869     7560       4120   800
#> Albertson College                    678          41    13500       3335   500
#>                              Personal PhD Terminal S.F.Ratio perc.alumni Expend
#> Abilene Christian University     2200  70       78      18.1          12   7041
#> Adelphi University               1500  29       30      12.2          16  10527
#> Adrian College                   1165  53       66      12.9          30   8735
#> Agnes Scott College               875  92       97       7.7          37  19016
#> Alaska Pacific University        1500  76       72      11.9           2  10922
#> Albertson College                 675  67       73       9.4          11   9727
#>                              Grad.Rate
#> Abilene Christian University        60
#> Adelphi University                  56
#> Adrian College                      54
#> Agnes Scott College                 59
#> Alaska Pacific University           15
#> Albertson College                   55

Created on 2019-12-26 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.