Maddocent
Maddocent

Reputation: 73

Change type of variables in multiple data frames

I have a list of data frames:

str(df.list)

List of 34
 $ :'data.frame':   506 obs. of  7 variables:
  ..$ Protocol   : Factor w/ 5 levels "P1","P2","P3",..: 1 1 1 1 1 1 1 1 1 1 ...
  ..$ Time       : num [1:506] 0 2 3 0.5 6 1 24 24 24 24 ...
  ..$ SampleID   : Factor w/ 40 levels "P1T0","P1T0.5",..: 1 5 7 2 8 3 6 6 6 6 ...
  ..$ VolunteerID: Factor w/ 15 levels "ID-02","ID-03",..: 10 10 10 10 10 10 10 11 13 14 ...
  ..$ Assay      : Factor w/ 1 level "ALAT": 1 1 1 1 1 1 1 1 1 1 ...
  ..$ ResultAssay: int [1:506] 23 23 23 24 25 24 20 34 28 17 ...
  ..$ Index      : Factor w/ 502 levels "P1T0.5VID-02",..: 8 31 37 2 43 19 25 26 28 29 ...
 $ :'data.frame':   505 obs. of  7 variables:
  ..$ Protocol   : Factor w/ 5 levels "P1","P2","P3",..: 1 1 1 1 1 1 1 1 1 1 ...
  ..$ Time       : num [1:505] 0 2 3 0.5 6 1 24 24 24 24 ...
  ..$ SampleID   : Factor w/ 40 levels "P1T0","P1T0.5",..: 1 5 7 2 8 3 6 6 6 6 ...
  ..$ VolunteerID: Factor w/ 15 levels "ID-02","ID-03",..: 10 10 10 10 10 10 10 11 13 14 ...
  ..$ Assay      : Factor w/ 1 level "ALB": 1 1 1 1 1 1 1 1 1 1 ...
  ..$ ResultAssay: int [1:505] 45 46 47 47 49 47 46 46 44 43 ...
  ..$ Index      : Factor w/ 501 levels "P1T0.5VID-02",..: 8 31 37 2 43 19 25 26 28 29 ..

The list contains 34 data frames with equal variable names. The variables Time and ResultAssay are of the wrong type: I would like to have Time as factor and ResultAssay as numerical.

I am trying to generate a function to use together with lapply to convert the variable type of this list of 34 data frames in one go, but so far i am unsuccessful.

I have tried things in parallel to:

ChangeType <- function(DF){

 DF[,2] <- as.factor(DF[,2])
 DF[, "ResultAssay"] <- as.numeric(DF[, c("ResultAssay")] 
}

lapply(df.list, ChangeType)

Upvotes: 1

Views: 2467

Answers (1)

anon
anon

Reputation:

What you have tried is nearly correct, but you also need to return the new data.frame and also store it to your existing variable, as so:

ChangeType <- function(DF){

 DF[,2] <- as.factor(DF[,2])
 DF[, "ResultAssay"] <- as.numeric(DF[, c("ResultAssay")]
 DF #return the data.frame 
}

# store the returned value to df.list, 
# thus updating your existing data.frame
df.list <- lapply(df.list, ChangeType) 

Upvotes: 3

Related Questions