将数据读入R表格式

时间:2018-04-29 22:34:55

标签: r

我想将这些数据读入R.当我使用read.csv命令读取时,它只创建了2列,而不是逐行读取数据并将数据放入表中。

以下是数据行的快照。enter image description here

这是我的R代码:

data = read.csv(file.choose(), header = TRUE, sep = ",", quote = "\"", dec = ".", fill = TRUE)

当前输出:

enter image description here

但是我想创建一个结构化的表,根据行中的位置,将每行的数据放入相关的行中。

谢谢!

1 个答案:

答案 0 :(得分:0)

这是来自Kaggle.com的video game sales data。要使用read.csv()进行阅读,请使用以下代码。请注意,我在答案中包含了前10行原始数据,因此它是可重现的。

vgsalesData <- "Rank,Name,Platform,Year,Genre,Publisher,NA_Sales,EU_Sales,JP_Sales,Other_Sales,Global_Sales
1,Wii Sports,Wii,2006,Sports,Nintendo,41.49,29.02,3.77,8.46,82.74
2,Super Mario Bros.,NES,1985,Platform,Nintendo,29.08,3.58,6.81,0.77,40.24
3,Mario Kart Wii,Wii,2008,Racing,Nintendo,15.85,12.88,3.79,3.31,35.82
4,Wii Sports Resort,Wii,2009,Sports,Nintendo,15.75,11.01,3.28,2.96,33
5,Pokemon Red/Pokemon Blue,GB,1996,Role-Playing,Nintendo,11.27,8.89,10.22,1,31.37
6,Tetris,GB,1989,Puzzle,Nintendo,23.2,2.26,4.22,0.58,30.26
7,New Super Mario Bros.,DS,2006,Platform,Nintendo,11.38,9.23,6.5,2.9,30.01
8,Wii Play,Wii,2006,Misc,Nintendo,14.03,9.2,2.93,2.85,29.02
9,New Super Mario Bros. Wii,Wii,2009,Platform,Nintendo,14.59,7.06,4.7,2.26,28.62
10,Duck Hunt,NES,1984,Shooter,Nintendo,26.93,0.63,0.28,0.47,28.31
"

sales <- read.csv(text=vgsalesData,header=TRUE,stringsAsFactors = FALSE)
head(sales)

...和输出:

> head(sales)
  Rank                     Name Platform Year        Genre Publisher NA_Sales EU_Sales JP_Sales
1    1               Wii Sports      Wii 2006       Sports  Nintendo    41.49    29.02     3.77
2    2        Super Mario Bros.      NES 1985     Platform  Nintendo    29.08     3.58     6.81
3    3           Mario Kart Wii      Wii 2008       Racing  Nintendo    15.85    12.88     3.79
4    4        Wii Sports Resort      Wii 2009       Sports  Nintendo    15.75    11.01     3.28
5    5 Pokemon Red/Pokemon Blue       GB 1996 Role-Playing  Nintendo    11.27     8.89    10.22
6    6                   Tetris       GB 1989       Puzzle  Nintendo    23.20     2.26     4.22
  Other_Sales Global_Sales
1        8.46        82.74
2        0.77        40.24
3        3.31        35.82
4        2.96        33.00
5        1.00        31.37
6        0.58        30.26
>