Reputation: 21
I am slowly teaching myself R, with a very basic background in programming. Producing plots etc. is relatively straightforward, but I am currently working on a small piece of code, with the intention of building a larger script for making processing of commercial lab results less onerous - these are typically issued in fairly disorganised CSV files, with a mixture of different units of concentration.
So, I am trying to produce a block of code which iterates through the rows of a CSV file containing four column headings: "Analyte", "Unit", "LOD", and "Concentration". I intend for my code to check which unit is being used for each analyte, and if it is mg/l, convert the concentration to ug/l for this analyte:
input_file <- read.csv(file="test.csv", header = TRUE,sep = ",")
apply(input_file, 1, function(row) {
if (input_file$Unit == "mg/l"){
input_file$Concentration <- input_file$Concentration*1000
}
}
)
print(input_file)
When I run this code (I am using Rstudio), I get the following error message: In if (input_file$Unit == "mg/l") { :
the condition has length > 1 and only the first element will be used
. I cannot find a solution to this, and my limited knowledge of programming jargon seems to be hindering this. Any ideas? Any suggestions, hints or resources would be much appreciated.
Upvotes: 1
Views: 65
Reputation: 521053
I believe this is what you were intending to do:
apply(input_file, 1, function(row) {
if (row$Unit == "mg/l"){
row$Concentration <- row$Concentration*1000
}
})
The temporary variable row
represents each row of the input file, and it is this which you mean to modify.
Here is a more efficient way to do this:
input_file[input_file$Unit == "mg/l", "Concentration"] <-
input_file[input_file$Unit == "mg/l", "Concentration"]*1000
Upvotes: 1