I want to generate multiple objects with R's calculations saved at once.

Asked 2 years ago, Updated 2 years ago, 33 views

Using R, the result of the calculation that was repeated several times (just extracting conditions from the data frame).I want to save ) to multiple objects. I've given a different list of conditions in columns every time, but I want to keep the data frame multi-row x multi-row structure, so I'm trying to rename the object every loop and save multiple objects.

Therefore, I wrote it using for as follows, but

for(i, in 1:length(selection1) {
Object=DataFrame [selection1[[i]],]
}

I'm having a hard time because I don't know how to change the name of the object for each loop.

If I want to change the name of an object in these for statements, how do I specify it?

r

2022-09-29 22:09

2 Answers

I'm not sure, but

set.seed(11)
dat<-data.frame(label1=rep(c("a", "b", "c"), each=10),
              label2 = rep(1:5,6),
              A = rnorm(30,1,3),
              B = rnorm(30,5,3))

This kind of data is

Make it a list.

 dat1<- NULL
for (i in 1:5) dat1 [[i]]<-dat [dat$label2==i,]

Or save them separately as csv.

list_name<-paste(1:5, ".csv", sep="")
for (i in 1:5) write.csv(dat[dat$label2==i,], list_name[i])

Does that mean...


2022-09-29 22:09

In addition to user23327, there is also a way to write using assign as follows

>results<-c("v", "w", "x", "y", "z")
>for(i in 1:5)assign(results[i], dat[dat$label2==i,])
>v
label1 label2 AB
1a 1-0.7730933 1.68714787
6a 1-1.80245406.45403114
11b 1-1.4852997 0.09045108
16b 11.03698093.96837696
21c 1-1.04755297.36961172
26c 11.0214764 6.62787928
>w
label1 label2 AB
2a 2 1.07978311 2.185549
7a24.970816944.441842
12b 2-0.04505518 5.061144
17b 20.33109138-1.560344
22c 20.95242542 4.310018
27 c 20.43719967 4.530065


2022-09-29 22:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.