【问题标题】:How to Separate Data Column in R如何在 R 中分隔数据列
【发布时间】:2014-02-18 23:14:05
【问题描述】:

我的数据结构为 4464 行和 1 列。 数据应在 4464 行和 8 列中。

数据包含: 每个文件有两个 行头,后面是数据。列是:朱利安天,十分钟 间隔标记、温度、压力、风速和风向。这 十分钟间隔标记是代表时间的 1 到 144 之间的数字。 数据来自here

这样的数据文件一共有 12 个,我的目标是把它们放在一个 3D 数组中。 但我坚持要修复这个数据表单。

数据示例

Jan  13   Station : 8900  Harry               
  Lat : 83.00S  Long : 121.40W  Elev :  957 M
    1    1   -7.8  879.0    5.6  360.0  444.0    9.1
    1    2   -7.9  879.1    4.6  360.0  444.0    9.1
    1    3   -7.6  879.2    4.1  345.0  444.0    9.1
    1    4   -7.6  879.3    4.1  339.0  444.0    9.1
    1    5   -7.6  879.4    4.8  340.0  444.0    9.1
    1    6   -7.9  879.4    3.6  340.0  444.0    9.1
    1    7   -8.0  879.3    4.6  340.0  444.0    9.1
    1    8   -8.0  879.4    4.1  340.0  444.0    9.1
    1    9   -8.2  879.4    5.8  338.0  444.0    9.1
    1   10   -8.4  879.5    4.6  339.0  444.0    9.1

我尝试并研究了一些东西,但我不知道最好的方法是什么。

我的代码是(无法使用 data.frame 进行编码...):

 setwd("/Users/Gizmo/Documents/Henry")
  dir()
  h13<-dir()
  henry<-read.csv(h13[1],skip=2,header=FALSE)
  colnames(c("J-Day","MinInter","Temp","Pressure","WindSpeed","WindDir","Ext1","Ext2"))

我查看了其他问题,guide 和 data.frame 似乎是最好的方法,但我不会编码。 (以数据维度 NULL 结束。)

请在这方面给我建议。谢谢。

【问题讨论】:

  • 这正是您的数据文件的样子吗?如果是这样,你想要read.table,而不是read.csv
  • 哦,CSV 是逗号分隔的,不是吗。谢谢!

标签: r formatting


【解决方案1】:

您的问题似乎是使用read.csv 而不是read.table

henry <- read.table(text='Jan  13   Station : 8900  Harry               
  Lat : 83.00S  Long : 121.40W  Elev :  957 M
    1    1   -7.8  879.0    5.6  360.0  444.0    9.1
    1    2   -7.9  879.1    4.6  360.0  444.0    9.1
    1    3   -7.6  879.2    4.1  345.0  444.0    9.1
    1    4   -7.6  879.3    4.1  339.0  444.0    9.1
    1    5   -7.6  879.4    4.8  340.0  444.0    9.1
    1    6   -7.9  879.4    3.6  340.0  444.0    9.1
    1    7   -8.0  879.3    4.6  340.0  444.0    9.1
    1    8   -8.0  879.4    4.1  340.0  444.0    9.1
    1    9   -8.2  879.4    5.8  338.0  444.0    9.1
    1   10   -8.4  879.5    4.6  339.0  444.0    9.1', header=FALSE, skip=2)
names(henry) <- c("J-Day","MinInter","Temp","Pressure","WindSpeed","WindDir","Ext1","Ext2")

结果:

> henry
   J-Day MinInter Temp Pressure WindSpeed WindDir Ext1 Ext2
1      1        1 -7.8    879.0       5.6     360  444  9.1
2      1        2 -7.9    879.1       4.6     360  444  9.1
3      1        3 -7.6    879.2       4.1     345  444  9.1
4      1        4 -7.6    879.3       4.1     339  444  9.1
5      1        5 -7.6    879.4       4.8     340  444  9.1
6      1        6 -7.9    879.4       3.6     340  444  9.1
7      1        7 -8.0    879.3       4.6     340  444  9.1
8      1        8 -8.0    879.4       4.1     340  444  9.1
9      1        9 -8.2    879.4       5.8     338  444  9.1
10     1       10 -8.4    879.5       4.6     339  444  9.1

> str(henry)
'data.frame':   10 obs. of  8 variables:
 $ J-Day    : int  1 1 1 1 1 1 1 1 1 1
 $ MinInter : int  1 2 3 4 5 6 7 8 9 10
 $ Temp     : num  -7.8 -7.9 -7.6 -7.6 -7.6 -7.9 -8 -8 -8.2 -8.4
 $ Pressure : num  879 879 879 879 879 ...
 $ WindSpeed: num  5.6 4.6 4.1 4.1 4.8 3.6 4.6 4.1 5.8 4.6
 $ WindDir  : num  360 360 345 339 340 340 340 340 338 339
 $ Ext1     : num  444 444 444 444 444 444 444 444 444 444
 $ Ext2     : num  9.1 9.1 9.1 9.1 9.1 9.1 9.1 9.1 9.1 9.1

【讨论】:

    猜你喜欢
    • 2020-08-28
    • 2021-03-01
    • 2021-11-17
    • 2018-07-18
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    相关资源
    最近更新 更多