2016-05-14 10 views
0

私はRを使って読む必要があるいくつかのデータを持つ単純なtxtファイルを持っています。列bは、データ用と同じ区切り文字(sep=",")で千単位の区切り,を含む金銭的価値である:あなたが見ることができるように金銭的価値を含むデータファイルの読み方R

a,  b,    c, e 
"1", €57,000.00,  5, 10FEB2015 
"K", €0.00,   6, 15APR2016 
"C", €1,444,055.00, 6, 15APR2016 

私のファイルには、これらの行が含まれています。

+0

を参照してください:http://stackoverflow.com/questions/1523126/how-to-read-data-when-some-numbers-コンマ - 千単位の区切り文字を含む?rq = 1 – Technophobe01

+0

ユーロの小数点(セント?)は常に「.00」ですか? –

+1

私は、金銭的価値の終わりを正しく見つけたら、それに引用符を加えてそれを普通に読むことができます。それは単に引用する必要があります。 –

答えて

2

時々あなたがライン・バイ・ラインそれをしなければならない。

library(stringi) 
library(purrr) 

lines <- 'a,b,c,e 
"1",€57,000.00,5,10FEB2015 
"K",€0.00,6,15APR2016 
"C",€1,444,055.00,6,15APR2016' 

dat <- readLines(textConnection(lines)) 

# we need the column names 
cols <- stri_split_regex(dat[1], ",")[[1]] 

# regular expression capture groups can do the hard work 
map_df(stri_match_all_regex(dat[2:length(dat)], 
        '^"([[:alnum:]]+)",€([[:digit:],]+\\.[[:digit:]]{2}),([[:digit:]]+),(.*)$'), 
    function(x) { 
    setNames(rbind.data.frame(x[2:length(x)], 
           stringsAsFactors=FALSE), 
      cols) 
    } 
) -> df 

# proper types 
df$b <- as.numeric(stri_replace_all_regex(df$b, ",", "")) 
df$e <- as.Date(df$e, "%d%b%Y") 

str(df) 

## Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 4 variables: 
## $ a: chr "1" "K" "C" 
## $ b: num 57000 0 1444055 
## $ c: chr "5" "6" "6" 
## $ e: Date, format: "2015-02-10" "2016-04-15" ... 
関連する問題