Analysis in R: “magicfor” package, which is useful for managing “for” command processing

RAnalytics

This package saves the results of each “for” command in a “list”, “data.frame” or “vector”. In particular, we recommend the “magic_result” command, which save the results in a “list”.

To use this method, simply enclose the processing content of the “for” command with the “magic_for” command and one of the “magic_result”, “magic_result_as_dataframe”, or “magic_result_as_vector” commands.

This is a very useful package. Just checking the contents of the “magic_for” command is helpful.

・”magicfor” package
https://github.com/hoxo-m/magicfor

Package version is 0.1.0. Checked with R version 4.2.2.

スポンサーリンク
Sponsored Link

Install Package

Run the following command.

#Install Package
install.packages("devtools")
devtools::install_github("hoxo-m/magicfor")

Example

See the command and package help for details.

#Loading the library
library("magicfor")

###Creating Data#####
n <- 100
TestData <- data.frame(Group = sample(paste0("Group", 1:5), n, replace = TRUE),
                       Data1 = rnorm(n),
                       Data2 = rnorm(n) + rnorm(n) + rnorm(n),
                       Data3 = sample(1:5, n, replace = TRUE),
                       Data4 = sample(LETTERS[1:26], n, replace = TRUE))
#######

###For example, store the results in a list#####
#Executed before "for" command:magic_for command
magic_for(print)

#"for" command
for(i in seq(unique(TestData[, 4]))) {
  
  Data <- subset(TestData, TestData[, 4] == i)
  print(Data)
  
}

#magic_result command
TestResult <- magic_result()

#check
str(TestResult)
List of 3
$ Data      :List of 5
..$ :'data.frame':   26 obs. of  5 variables:
  $ ExCharData:List of 5
..$ : chr "T"
#省略
$ Char      :List of 5
..$ :'data.frame':   7 obs. of  5 variables:
  
#Get in "data.frame": magic_result_as_dataframe command
TestResultDF <- magic_result_as_dataframe()
#内容を確認
TestResultDF

I hope this makes your analysis a little easier !!

Copied title and URL