Reputation: 1215
I have produced 10 nested lists within lists using two functions (below) - produced from dummy data called normalised_scores
. Each nested list contains two empty columns called Predicted
and Actual
(code below). I would like to replace these empty columns with NA's. I tried to achieve this with a function (Shuffle100
) but got error messages (below)
. After looking online for a long time, I cannot find an efficient method apart from using long-winded code (below). If anyone can help, then thank you.
Family=rep(c("G8", "V4"), each=40)
x <- matrix(rnorm(960), ncol=12)
normalised_scores <- cbind(Family, x)
colnames(my_data)<-c("Family",
"Swimming",
"Not.Swimming",
"Running",
"Not.Running",
"Fighting",
"Not.Fighting",
"Resting",
"Not.Resting",
"Hunting",
"Not.Hunting",
"Grooming",
"Not.Grooming")
I wrote this function below to produce these 10 nested lists from classification tree models using rpart
and caret
packages. I tried to produce two columns (Predicted
and Actual
) filled with NA's
using this function but without success.
library(caret)
library(e1071)
library(rpart)
shuffle100 <-lapply(seq(10), function(n){ #Select the production of 10 dataframes
subset <- normalised_scores[sample(nrow(normalised_scores), 80),] #Shuffle rows
subset_idx <- sample(1:nrow(subset), replace = FALSE)
subset <- subset[subset_idx, ] #training subset
subset1<-subset[-subset_idx, ] #test subset
subset_resampled_idx <- createDataPartition(subset_idx, times = 1, p = 0.7, list = FALSE) #70 % training set
subset_resampled <- subset[subset_resampled_idx, ]
ct_mod<-rpart(Family~., data=subset_resampled, method="class", control=rpart.control(cp=0.005)) #10 ct
ct_pred<-predict(ct_mod, newdata=subset[, 2:13])
ct_dataframe=as.data.frame(ct_pred) #create new data frame
ct_dataframe$Predicted=NA
ct_dataframe$Actual=NA
})
Produce two empty columns called Predicted and Actual
my_list <- lapply(shuffle100, function(df){#Create two new columns Predicted and Actual
if (nrow(df) > 0)
cbind(df, Predicted = c(""), Actual = c(""))
else
cbind(df, Predicted = character(), Actual = c(""))
})
Error messages from the function Shuffle100
and much of the same (lists 1-10):
[[1]]
[1] NA
[[2]]
[1] NA
[[3]]
[1] NA
Is there a more efficient way of doing this?
#Insert NA's
my_list[[1]]$Predicted<-NA
my_list[[1]]$Actual<-NA
my_list[[2]]$Predicted<-NA
my_list[[2]]$Actual<-NA
my_list[[3]]$Predicted<-NA
my_list[[3]]$Actual<-NA
my_list[[4]]$Predicted<-NA
my_list[[4]]$Actual<-NA
my_list[[5]]$Predicted<-NA
my_list[[5]]$Actual<-NA
my_list[[6]]$Predicted<-NA
my_list[[6]]$Actual<-NA
my_list[[7]]$Predicted<-NA
my_list[[7]]$Actual<-NA
my_list[[8]]$Predicted<-NA
my_list[[8]]$Actual<-NA
my_list[[9]]$Predicted<-NA
my_list[[9]]$Actual<-NA
my_list[[10]]$Predicted<-NA
my_list[[10]]$Actual<-NA
Upvotes: 1
Views: 383
Reputation: 887991
We can use transform
lapply(my_list, transform, Predicted=NA, Actual=NA)
Upvotes: 0
Reputation: 159
Using dplyr's mutate function, try
library(dplyr)
lapply(my_list, function(x) mutate(x, Predicted = NA, Actual = NA)
Upvotes: 2