nzhanggh
nzhanggh

Reputation: 131

Trouble reading a messy CSV file in R

I have been trying to read a CSV into R. The CSV is separated in a strange way with all values within one column separated by commas like in this picturethis picture. The top row is the column names and then below are the values When I try read_csv("filename") nothing shows up in the tibble except a bunch of NA values like in this picture after running the view function enter image description here. How can I approach this?

Here is the data for reference

, Calories, Fat (g), Carb. (g), Fiber (g), Protein (g)
Chonga Bagel,300,5,50,3,12
8-Grain Roll,380,6,70,7,10
Almond Croissant,410,22,45,3,10
Apple Fritter,460,23,56,2,7
Banana Nut Bread,420,22,52,2,6
Blueberry Muffin with Yogurt and Honey,380,16,53,1,6
Blueberry Scone,420,17,61,2,5
Butter Croissant,240,12,28,1,5
Butterfly Cookie,350,22,38,0,2
Cheese Danish,320,16,36,1,8
Chewy Chocolate Cookie,170,5,30,2,2
Chocolate Chip Cookie,310,15,42,2,4
Chocolate Chunk Muffin,440,21,60,2,7
Chocolate Croissant,330,18,38,1,6
Chocolate Hazelnut Croissant,390,22,43,2,7
Chocolate Marble Loaf Cake,490,24,64,2,6
Cinnamon Morning Bun,390,15,56,2,8
Cinnamon Raisin Bagel,270,1,58,3,9
Classic Coffee Cake,390,16,57,1,5
Cookie Butter Bar,360,23,36,0,2

Upvotes: 0

Views: 433

Answers (1)

UseR10085
UseR10085

Reputation: 8146

Use the following code to read the data

df = read.csv("starbucks-menu-nutrition-food.csv", skipNul = T)

head(df, 2)

        ÿþ Calories Fat..g. Carb...g. Fiber..g. Protein..g.
1 Chonga Bagel      300       5        50         3          12
2 8-Grain Roll      380       6        70         7          10

Then you may consider renaming the columns like for e.g.

colnames(df) <- c("Food", "Calories", "Fat", "Carb", "Fiber", "Protein")

for further processing of the data.

Upvotes: 2

Related Questions