2016-08-09 7 views
-1

私は、次の.csvファイルを持っている:グラブテーブルから文字列とRの列として追加

https://drive.google.com/open?id=0Bydt25g6hdY-RDJ4WG41VFpyX1k

そして、私は(その構成部分を貼り付け)、日付とエージェント名を取ることができるようにしたいと思います

enter image description here

のみ:それは別の名前や日付、次のような結果を得るために、残りの名前と日付の項目に同じことをやって見つけるまで、最大、テーブルの右側に列としてそれらを追加私のことAVE dplyrパッケージで行うことができては、次のとおりです。

私に次のような結果与え
library(dplyr) 
library(stringr) 


report <- read.csv(file ="test15.csv", head=TRUE, sep=",") 

date_pattern <- "(\\d+/\\d+/\\d+)" 
date <- str_extract(report[,2], date_pattern) 

report <- mutate(report, date = date) 

:私は発見しています難易度は、おそらくスクリプトを作るために、条件文を使用している

enter image description here

を適切な文字列を取得し、テーブルの最後に列として追加します。

答えて

1

これは粗悪かもしれませんが、私はいくつかのことを説明していると思います:a)stringsAsFactors=F; b)データフレーム内の列を「事前に割り当てる」。 c)列番号の代わりに列名を使用して値を設定します。

report<-read.csv('test15.csv', header=T, stringsAsFactors=F) 

# first, allocate the two additional columns (with NAs) 
report$date <- rep(NA, nrow(report)) 
report$agent <- rep(NA, nrow(report)) 

# step through the rows 
for (i in 1:nrow(report)) { 
    # grab current name and date if "Agent:" 
    if (report[i,1] == 'Agent:') { 
    currDate <- report[i+1,2] 
    currName=paste(report[i,2:5], collapse=' ') 
    # otherwise append the name/date 
    } else { 
    report[i,'date'] <- currDate 
    report[i,'agent'] <- currName 
    } 
} 
write.csv(report, 'test15a.csv') 

Output file

+0

これは私が必要とするまさに行い、そしてイラストエドワードありがとうございました! – Carlos

関連する問題