I have multiple datasets that contain the same names of variables but different values, i am trying to create a new variable called group that will distinguish every set of datasets (i am doing this because i will put all the datasets in one and i can distinguish by group). Here is a sample for 2 datasets.
################################
### Sample data ###
################################
set.seed(8547)
a=sample(1:20,15,replace=FALSE)
a=sort(a)
f=runif(15,0,1)
f=sort(f)
trt1=data.frame(a,f)
set.seed(1452)
a=sample(1:35,22,replace=FALSE)
a=sort(a)
f=runif(22,0,1)
f=sort(f)
trt2=data.frame(a,f)
names_of_dataframes <- ls.str(mode = "list")
# I used a `for` loop because i have approximatively `10` datasets and i do not know if the `apply` family would work for this kind of treatment
for (i in length(names_of_dataframes)) {
if(names_of_dataframes[i]=="trt1"){
trt1$group=rep("trt1",nrow(trt1))
}else if (names_of_dataframes[i]=="trt2"){
trt2$group=rep("trt2",nrow(trt2))
}
}
I do not know what i am doing wrong but the group variable is only created for dataset trt2 and not trt1. Any thoughts what is wrong?
Thank you in advance for your help