1

I have a list named which consists of 9 character type elements.

>dput(mylist)
list("COG_ONTOLOGY", "GOTERM_BP_DIRECT", "GOTERM_CC_DIRECT", 
"GOTERM_MF_DIRECT", "INTERPRO", "KEGG_PATHWAY", "PIR_SUPERFAMILY", 
"SMART", "UP_KEYWORDS")

I also have 9 dataframes with the same names as the elements in . For example, the dataframe COG_ONTOLOGY has the following structure.

COG_ONTOLOGY <- data.frame(
  name = c("A", "B", "C", "D", "E"),
  value = as.numeric(0))

In the dataframe COG_ONTOLOGY, I need to increment the value of A by 1, but without specifying the name of the dataframe and "A". I tried this but it doesn't seem to work.

assign(get(mylist[[1]])[1, "value"], get(mylist[[1]])[1, "value"] + 1)
accibio
  • 487
  • 2
  • 4
  • 13
  • Please provide a [reproducible minimal example](https://stackoverflow.com/q/5963269/8107362). Especially, provide all sample data in a ready-to-copy format, e.g. with `dput()` and use the reprex-package. – mnist Aug 22 '21 at 15:35
  • Are the data frame separate entities from the list, i.e. the list is a list of one element names. Whereas the data frames are 9 separate objects? – Peter Aug 22 '21 at 15:45
  • @Abhisek, to increment the value of A by 1, doesn’t seem a very meaningful action. Maybe you could consider restate the action you want to execute over the data frames. – jamoreiras Aug 22 '21 at 16:12

2 Answers2

1

It would help to have confirmation of your expected output.

Here is one way to approach what I think you are trying to achieve

``` r
COG_ONTOLOGY <- data.frame(
  name = c("A", "B", "C", "D", "E"),
  value = as.numeric(0))


my_list <- as.list(unlist(c("COG_ONTOLOGY", "GOTERM_BP_DIRECT")))

df <- get(my_list[[1]])

df$value[df$name == "A"] <- df$value[df$name == "A"] + 1

assign(my_list[[1]], df)

COG_ONTOLOGY
#>   name value
#> 1    A     1
#> 2    B     0
#> 3    C     0
#> 4    D     0
#> 5    E     0

Created on 2021-08-22 by the reprex package (v2.0.0)

Peter
  • 11,500
  • 5
  • 21
  • 31
  • This solution changes the value associated with A only in the dataframe 'df' and not in the original dataframe 'COG_ONTOLOGY'. Is there a way to get the changes done in the 'COG_ONTOLOGY' dataframe? – accibio Aug 22 '21 at 18:15
  • Updated answer, hopefully sufficiently. – Peter Aug 22 '21 at 19:28
0

We may access components dataframes in lists by indices:

  1. To access top-level components of a list of data frames use a double slicing operator [[ ]]
  2. to access lower or inner level components of a list use another square [ ] along with the double slicing operator [[ ]]

Here is an example:

COG_ONTOLOGY <- data.frame(
    name = c("A", "B", "C", "D", "E"),
    value = as.numeric(0))

GOTERM_BP_DIRECT <- data.frame(
    name = c("F", "G", "H", "I", "J"),
    value = as.numeric(0))

mylist <- list(COG_ONTOLOGY, GOTERM_BP_DIRECT)
mylist
mylist[[1]][1,2] <- mylist[[1]][1,2]+1
mylist


> mylist
[[1]]
  name value
1    A     1
2    B     0
3    C     0
4    D     0
5    E     0

[[2]]
  name value
1    F     0
2    G     0
3    H     0
4    I     0
5    J     0
TarJae
  • 72,363
  • 6
  • 19
  • 66