将空格分隔的数据集加载到R中,而不删除字符串之间的空格

pkln4tw6  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(119)

我有一个很大的CSV文件,其中一行的所有值都在一个由空格分隔的单元格中。此数据是数值和字符串的混合。当我加载数据时,它将单个变量的字符串拆分为多个列。

数据结构示例:

Survey "SiteNo" "CODE" "Species_name" "Length" "Sex" "Age" "Station" "lonitude" "latitude" "Date" "Weight"  
1 "T 19/11" 45 "MMM" "Gadus Morhua" 80 "U" NA "F5" -7.979 50.837 2011-11-14 4   
2 "T 20/14" 20 "MNE" "Homarus Gammarus" 75 "U" "0" 1 -2.501 50.216 2014-10-02 3

其中每行是一行(单个单元格中每行的所有数据)

我需要的数据看起来像:

| 调查|站点编号|嗯|物种名称|长度|性|年龄|站|孤独|纬度|日期|重量|
| --|--|--|--|--|--|--|--|--|--|--|--|
| T 19/11| 45 |嗯|鳕Morhua| 80 |U| 1 |F5| -7.979 |50.837| 2011-11-14 2011 -11-14| 4 |
| T 20/14| 20 |MNE|霍马鲁斯| 75 |U| 0 | 1 | -2.501 |50.216| 2014-10-02 2014-10-02| 3 |
欢呼

mccptt67

mccptt671#

read.table中使用sep=' '

r <- read.table('foo.csv', sep=' ')
r
#    Survey SiteNo CODE     Species_name Length Sex Age Station lonitude latitude       Date Weight
# 1 T 19/11     45  MMM     Gadus Morhua     80   U  NA      F5   -7.979   50.837 2011-11-14      4
# 2 T 20/14     20  MNE Homarus Gammarus     75   U   0       1   -2.501   50.216 2014-10-02      3

哪里

str(r)
# 'data.frame': 2 obs. of  12 variables:
#  $ Survey      : chr  "T 19/11" "T 20/14"
#  $ SiteNo      : int  45 20
#  $ CODE        : chr  "MMM" "MNE"
#  $ Species_name: chr  "Gadus Morhua" "Homarus Gammarus"
#  $ Length      : int  80 75
#  $ Sex         : chr  "U" "U"
#  $ Age         : int  NA 0
#  $ Station     : chr  "F5" "1"
#  $ lonitude    : num  -7.98 -2.5
#  $ latitude    : num  50.8 50.2
#  $ Date        : chr  "2011-11-14" "2014-10-02"
#  $ Weight      : num  4 3
  • .csv内容 *
Survey "SiteNo" "CODE" "Species_name" "Length" "Sex" "Age" "Station" "lonitude" "latitude" "Date" "Weight"  
1 "T 19/11" 45 "MMM" "Gadus Morhua" 80 "U" NA "F5" -7.979 50.837 2011-11-14 4   
2 "T 20/14" 20 "MNE" "Homarus Gammarus" 75 "U" "0" 1 -2.501 50.216 2014-10-02 3

相关问题