I am new to R. I normally use Python. Let's say I have a list of 100 items. Each item is a list of 3 items. This is the data structure.
List > Companies > Date, Revenues, Expenses
I can retrieve information using:
list$companyA$revenue
I want to iterate through the 100 companies.
When I used this code, I end up getting everything printed:
for(i in 1:length(list)) {
print(list[i])
}
I have also tried:
lapply(list, `[[`, 1)
This gives the 100 companies and the first list item, the date.
I want to put this information into a function:
Function(list$company[1])
And I want to iterate through each company, so I can automatically do
Function(list$companyA[1])
Function(list$companyB[1])
etc
I can't seem to find the right answer. Maybe I am searching for it in the wrong way.
EDIT: To be clear, I want a list of the companies, so I can iterate through the companies.
EDIT2: Here is an example data:
Company Date Revenues Expenses
A 08012022 100 100
A 08022022 102 100
B 08012022 200 150
B 08012022 202 150
To get it into a list of lists, I used
list <- lapply(split(clean, clean$Company), as.list)
Now, from my understanding of Python, I want a list of my companies,so I can iterate through them and put them in a function:
firms = ("A, B")
So I can iterate through each company and replace it in the function.
My thought is then to have something like (again from Python):
for (firm in firms) {
Function(list$firm[2], list$firm[3])
}
CodePudding user response:
You could use names()
to extract the list names, and iterate them in the loop.
firms <- names(list)
for (firm in firms) {
Function(list[[firm]][2], list[[firm]][3])
}
Note that you cannot use list$firm
in the for loop. The $
works only if list
has an element named firm
. In the loop you should use list[[firm]]
.
CodePudding user response:
If you want to compare R and Python...here you go.
I've put your data through dput()
. You should do this or a reprex
when you ask R questions. (You'll get answers faster.)
firm <- structure(list(Company = c("A", "A", "B", "B"),
Date = structure(c(19000, 19031, 19000, 19000),
class = "Date"),
Revenues = c(100L, 102L, 200L, 202L),
Expenses = c(100L, 100L, 150L, 150L)),
class = "data.frame", row.names = c(NA, -4L))
Now, because you were asking about lists, I converted this data frame to a list.
# make this data frame a list, by rows
frm2 <- split(firm, seq(nrow(firm)))
You can use a for statement the same way in R. This will print out the 1st and 2nd indices in each list. The double brackets let me keep the date formatting.
The variable firms
does not need to exist before making this call.
for(firms in frm2){
message(firms[1], ' ', firms[[2]])
}
A 2022-01-08
A 2022-02-08
B 2022-01-08
B 2022-01-08
Or see it all
for(firms in frm2){
message("In the company, ", firms[1], ", you'll see that on ",
firms[[2]], " Revenue was $", firms[3],
" & Expenses were $", firms[4])
}
# In the company, A, you'll see that on 2022-01-08 Revenue was $100 & Expenses were $100
# In the company, A, you'll see that on 2022-02-08 Revenue was $102 & Expenses were $100
# In the company, B, you'll see that on 2022-01-08 Revenue was $200 & Expenses were $150
# In the company, B, you'll see that on 2022-01-08 Revenue was $202 & Expenses were $150