Sydney
Sydney

Reputation: 5

R Data.Frame...Lots of Dots

Today is my first time using R in a while, so I decided to do a few exercises to see what I remember. Unfortunately, it seems I've failed to complete the most basic of tasks: creating a data frame.

Here is my code:

colleges <- data.frame(
  name <- c("Drexel University", "University of Pittsburgh", "Pennsylvania State University"),
  tuition <- c("$49,632", "$16,952", "$18,618")
)

And the output:

# >colleges
  name....c..Drexel.University....University.of.Pittsburgh....Pennsylvania.State.University..
1                                                                           Drexel University
2                                                                    University of Pittsburgh
3                                                               Pennsylvania State University
  tuition....c...49.632.....16.952.....18.618..
1                                       $49,632
2                                       $16,952
3                                       $18,618`

Why is it formatted so poorly? And what is the meaning of all those unnecessary dots?

Upvotes: 0

Views: 49

Answers (2)

MrFlick
MrFlick

Reputation: 206401

Use

colleges <- data.frame(
  name = c("Drexel University", "University of Pittsburgh", "Pennsylvania State University"),
  tuition = c("$49,632", "$16,952", "$18,618")
)

Note the use of = rather than <-. This names the parameters you are passing to data.frame and those parameter names are used as the column names. When you do an assign (<-) those parameters are unnamed so it deparses the expression to name it and you don't get a "pretty" name that way. For example

make.names(deparse(quote(tuition <- c("$49,632", "$16,952", "$18,618"))))
# [1] "tuition....c...49.632.....16.952.....18.618.."

Upvotes: 4

akash87
akash87

Reputation: 3994

You should use "=" in the data.frame call

colleges <- data.frame(name = c("Drexel University", "University of Pittsburgh", "Pennsylvania State University"),
                       tuition = c("$49,632", "$16,952", "$18,618"))

Upvotes: 1

Related Questions