我有一個簡單的.csv格式的數據,需要先處理,然後才能創建符合此數據的圖。但是,我知道如何從python操作.csv格式的數據。我想在R中應用相同的邏輯,但我不知道如何做到這一點。在R中的.csv數據操作而不是python
下面是來自.csv文件但加載到R的示例數據。我爲我們創建了代碼來討論此問題。
df <- data.frame(Name = c("AC", "AC", "PT", "PT", "OR", "OR"),
useless_column = c("","","A",3,4," "),
measurement = c("H", "", "K", "M", "", "H"),
amount = c(12, 54, 20, 87, 75, 22),
useless_column = c("","","A",3,4," "))
在Python中,我將通常這樣做:
import csv
import os
import glob
import sys
fileList = glob.glob("R:xxxxxxxxxxxxxxxxxxxxx\*.csv")
for inputFile in fileList:
outputFilename = inputFile + "output.csv"
csvInput = csv.reader(open(inputFile,'r'),delimiter=",")
outputFile = open(outputFilename,'w')
outputFile.write("Name,measurement,amount\n")
csvInput.next()
for line in csvInput:
if line[2] == "H":
meas = "100"
elif line[2] == "K":
meas = "1000"
elif line[2] == "M":
meas = "1000000"
else:
meas = "1"
amount = int(meas) * line[3]
outputFile.write(",".join(line[0],line[2],amount+"\n"]))
outputFile.close()
在Python中,我可以加載CSV,然後使用for循環來識別從csv文件的每一行的。然後在繼續我的分析之前定製我的輸出文件。從上面,我希望我的輸出像下面的代碼是在R格式:
df <- data.frame(Name = c("AC", "AC", "PT", "PT", "OR", "OR"),
measurment = c("H", "", "K", "M", "", "H"),
amount = c(1200, 54, 20000, 87000000, 75, 2200))
我想知道這樣做在R?我有R和普萊舍的人的一個小代碼可以指引我到正確的方向:R中
x <- read.csv("xxxx.csv", header=T,sep=",")
xC = ncol(x)
xR = nrow(x)
op = data.frame(matrix(data = x, nrow= xR, ncol=3,byrow=T))
for (x in :xC)
{
for (r in 1:xR)
{
xxxxxxxx
}