2017-05-15 7 views
1

インポートする複数のデータ(excelまたはcsv)に関する質問があります。私は、同時に複数のExcelデータ(同じカラム名を)読みたい場合は、私が知っている
は、コードは次のとおりです。フォーマットを使用してデータをインポート

library(readxl) 
file.list <- dir(path = "/path", pattern='\\.xlsx', full.names = T) 
df.list <- lapply(file.list, read_excel) 
data <- rbindlist(df.list) 

だから、私はそれらを同時に読んで、一つのデータに結合します。
しかし、私はデータの1つを例として取り上げています。 Excelでは
ROW1は、タイトル名とがheaderあるROW2を持っているので、obs.ROW3から始まります。また
、最初のデータは、EXCELで次のようになります場合:

TITLE~~~~~ 
col1 col2 col3 
A  1  3.59283E+14 
B  2  3.59258E+14 
C  3  3.59286E+14 
REFUND 
A  -1  3.59286E+14 

しかし、col3は、Excelで数値として定義されます。

TITLE~~~~~ 
col1 col2 col3 
A  1  359283060959987 
B  2  359258069826064 
C  3  359286062903911 
REFUND 
A  -1  359283060959987 

Row6REFUNDを持っている:実際には、それは次のようになります。私の複数のデータでは、どの行にREFUNDがあるのか​​わかりません。私は私のobsを読んでみたい。これらの行はありません。どのようにできるのか?
実際には、col3characterです。しかし、Excelではnumericのように見えます。
Rにインポートするときに、characterと定義すると、インポート後に指数記号が表示されません。

答えて

2

少なくとも、read_excel機能でREFUND行なしで直接データを読み取る方法はないと思います。 しかし、私はRでかなり新しいです。私は間違っている可能性があります。

しかし、私にとって最初に起こることは、あなた自身の機能を構築することです。下の方がうまくいくようです。

library(readxl) 
library(data.table) 

file.list <- dir(path = ".", pattern='\\.xlsx', full.names = T) 

my_read_data<-function(x){ #x list of files 

    df.list<- lapply(x, function(x){read_excel(path=x,skip=1,col_names = TRUE, 
              col_types=c("text","numeric","text"))}) 
    #skip -> skip the line with the title 
    #col_names -> use the first row as column names, i.e., col1, col2 and col3 
    #col_types-> vector containing one entry per column indicating the type of data 

    my.data <- rbindlist(df.list) 
    my.data.clean<-my.data[my.data$col1!="REFUND",] #select only rows without "REFUND" 

    return(my.data.clean) 
} 

機能を実行するために、私はREFUND行の位置を変更して、Excelの例を4回複製しています。私が得た結果は、次のように です。

the.data<-my_read_data(file.list) 

>the.data 
    col1 col2   col3 
1: A 1 359283060959987 
2: B 2 359258069826064 
3: C 3 359286062903911 
4: A -1 359283060959987 
5: A 1 359283060959987 
6: B 2 359258069826064 
7: C 3 359286062903911 
8: A -1 359283060959987 
9: A 1 359283060959987 
10: B 2 359258069826064 
11: C 3 359286062903911 
12: A -1 359283060959987 
13: A 1 359283060959987 
14: B 2 359258069826064 
15: C 3 359286062903911 
16: A -1 359283060959987 

EDIT - 機能あなたのコメントについては文字型

に変更される列を渡すために、多分あなたの代わりにこの機能を検討することができますしたいと思います:

my_read_data2<-function(x,character_col=NULL){ #x->list of files 
               # character_col->column to be change to character 
               # can be more than one 

    df.list<- lapply(x, function(x){read_excel(path=x,skip=1,col_names = TRUE)}) 
    my.data <- rbindlist(df.list) 
    my.data.clean<-my.data[my.data$col1!="REFUND",] #select only rows without "REFUND" 

    # changing column selected by character_col to character 
    # since the result from step above is a data table, 
    # access to elements is different from data frame 

    if(!is.null(character_col)){ #this allow you to use the function using only 
           # default results from read_excel 

     my.data.clean[, eval(character_col):= lapply(.SD, as.character), 
        .SDcols= character_col] 
    } 
    # eval -> you need to evaluate the argument you pass to the function, 
    #   otherwise you'll end up with an additional character_col column 
    #   that will be a list of all the columns you include in .SDcols 
    #.SD -> is the subset of the data table, in this case 
    #  .SDcols specifies the columns that are included in .SD. 

    return(my.data.clean[]) # in that case, don't forget the [] to avoid 
          #the odd behaviour when calling your resulting data table 
          #(see link at the end)        
} 

例:

the.data<-my_read_data2(file.list) 
str(the.data) 

>str(the.data) 
    Classes ‘data.table’ and 'data.frame': 16 obs. of 3 variables: 
    $ col1: chr "A" "B" "C" "A" ... 
    $ col2: num 1 2 3 -1 1 2 3 -1 1 2 ... 
    $ col3: num 3.59e+14 3.59e+14 3.59e+14 3.59e+14 3.59e+14 ... 
    - attr(*, ".internal.selfref")=<externalptr> 

the.data1<-my_read_data2(file.list,"col3") 
str(the.data1) 

> str(the.data1) 
    Classes ‘data.table’ and 'data.frame': 16 obs. of 3 variables: 
    $ col1: chr "A" "B" "C" "A" ... 
    $ col2: num 1 2 3 -1 1 2 3 -1 1 2 ... 
    $ col3: chr "359283060959987" "359258069826064" "359286062903911" "359283060959987" ... 
    - attr(*, ".internal.selfref")=<externalptr> 

複数の列:

the.data2<-my_read_data2(file.list,c("col2","col3")) 
the.data3<-my_read_data2(file.list,c(2,3))  

data.table objects not printed after returned from function

は、これは素晴らしいですあなた

+1

お役に立てば幸いです。 'col_types'に関するもう少し小さな質問です。列名を指定できますか?like: 'col_types = c(" col3 "=" text ")' –

+2

こんにちは@Peter Chenは既に述べたように私は専門家ではありませんが、col_typesはポジションベースでしか動作しません。 つまり、型はベクトルによって提供される順序に従って割り当てられます。実際にあなたが持っていたならば、6つの列と3つのタイプだけが定義されているとしましょう。これらの3つのタイプは、最後の3つの列のリサイクルです。 read_excelで型を推測し、後で変数型を変換できるようにデータをインポートすることはできます。 –

関連する問題