今天是我第一次使用R,所以我决定做一些练习,看看我记得什么。不幸的是,我似乎未能完成最基本的任务:创建数据框架。
这是我的代码:
colleges <- data.frame(
name <- c("Drexel University", "University of Pittsburgh", "Pennsylvania State University"),
tuition <- c("$49,632", "$16,952", "$18,618")
)
输出:
# >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`
为什么格式这么糟糕?那些不必要的点是什么意思?
答案 0 :(得分:4)
使用
colleges <- data.frame(
name = c("Drexel University", "University of Pittsburgh", "Pennsylvania State University"),
tuition = c("$49,632", "$16,952", "$18,618")
)
请注意使用=
而不是<-
。这将命名要传递给data.frame的参数,并将这些参数名称用作列名。当您进行分配(<-
)时,这些参数是未命名的,因此它会删除表达式以命名它,并且您不会得到一个非常好的参数。这样命名。例如
make.names(deparse(quote(tuition <- c("$49,632", "$16,952", "$18,618"))))
# [1] "tuition....c...49.632.....16.952.....18.618.."
答案 1 :(得分:1)
你应该使用&#34; =&#34;在data.frame
电话
colleges <- data.frame(name = c("Drexel University", "University of Pittsburgh", "Pennsylvania State University"),
tuition = c("$49,632", "$16,952", "$18,618"))