ITの隊長のブログ

ITの隊長のブログです。Rubyを使って仕事しています。最近も色々やっているお(^ω^ = ^ω^)

【R】CSVファイルじゃなくて、文字列からデータを作りたいとき

R言語デビュー

# 文字列を変数にいれる
lines <- "price,name,need
42504,credit card,0
14000,pc loan,0
10000,debt,0
1800,server price,1
1200,use internet price,0
6500,study group,1
20000,gasoline,0
2000,hotel,1
7000,event,1
8000,highway per month,1
10000,remittance to the parent,0
14000,friend,0"

# ここがみそ
con <- textConnection(lines)
data <- read.csv(con)
close(con)

# 金額合計を出力
sum(data$price)

# need列が0のやつだけ集計
sum(data$price[data$need == 0])

こうすればできた。ぼちぼちがんばる。