1

I have a list of objects containing different numbers of data, on which I want to globally execute the same round function. Is there a way to do it in one go whilst keeping the objects separate (so that each object corresponds to the data it should contain), without having to go do it manually as follows?

my code when trying to manually iterate the same function for each object

Thanks, Katie

Edit1:

> D41
[1] 1.366 1.223 1.223 2.703 1.205 2.053
> d44
[1] 8.929 13.793
> D41r <- round(D41,2)
> d44r <- round(d44,2)
> D41r
[1] 1.37 1.22 1.22 2.70 1.21 2.05
> d44r
[1] 8.93 13.79

Here I just showed two objects out of the many more objects each with a different number of data.

Is there any way to do create a data frame that has "different number of rows", or to execute one function for each object overwriting itself correspondingly?

katieluii
  • 9
  • 3
  • Welcome to SO! Please provide a [reproducible minimal example](https://stackoverflow.com/q/5963269/8107362). Especially, provide some sample data, e.g. with `dput()` and your code within your question. By this, the chances are much higher cou get some help. – mnist May 08 '20 at 00:01
  • Sorry about that @all who answered, I am quite new to both R and stack overflow! So an example would be as follows: > D41 [1] 1.366 1.223 1.223 2.703 1.205 2.053 > d44 [1] 8.929 13.793 > D41r <- round(D41,2) > d44r <- round(d44,2) > D41r [1] 1.37 1.22 1.22 2.70 1.21 2.05 > d44r [1] 8.93 13.79 Here I just showed two objects. But I have more objects with variable number of data. Is there any way to do create a data frame that has "different number of rows", or to execute one function for each object overwriting itself correspondingly? – katieluii May 08 '20 at 10:32
  • please rather edit your question and take care of code formatting – mnist May 08 '20 at 11:22
  • once again. Please edit your question rather than posting comments. – mnist May 08 '20 at 11:37
  • @mnist yes sorry, I realised I could not add a code with linebreaks in the comments. I didn't realise I can edit my question - thanks. – katieluii May 08 '20 at 11:40

3 Answers3

0

Base R solution:

lapply(obj_list, function(x){if(length(grep("D\\d+", names(x))) > 0){round(x, 3)}else{x}})
hello_friend
  • 5,682
  • 1
  • 11
  • 15
0

Based on your screenshot it looks like the issue is your objects are not in a list that you can easily work with, but simply stored as values in your global environment (or .GlobalEnv). Here is a workaround using ls() and getElement:

lapply(ls(), function(x) {
  new_value <- round(getElement(.GlobalEnv, x))
  assign(x, new_value, pos = .GlobalEnv) # re-assign its value to the rounded value
})

ls() will list the names of the values and objects (everything) in .GlobalEnv (your global environment). You may need to use grep to select just the ones you want, it depends what else is in there.

With getElement you can then supply the names of the objects as character strings (which is how ls() will return them, and extract their values from whence they exist. So your first argument to getElement is .GlobalEnv, the location where the objects live. And the second argument is the name of the object. If you then want to change its value in the global environment, then you can use the assign function to essentially overwrite it, which similarly allows you to specify the name of the value, as a character string, and then .GlobalEnv as the position argument if the object simply lives in your global environment.

Dij
  • 1,318
  • 1
  • 7
  • 13
0

Since I couldn't grab your data structure, I built a similar one:

dfD <- data.frame(D = paste0("D", sample(14:64, 12)),
              Dval = rnorm(12, mean = 20))

R > dfD
     D     Dval
1  D25 20.90942
2  D53 21.51484
3  D59 18.11695
4  D44 20.29304
5  D57 19.51578
6  D22 21.72018
7  D16 20.62322
8  D31 21.97484
9  D18 19.26036
10 D61 22.08744
11 D37 19.36332
12 D55 20.67799

Then I rounded all the Dval to three digits:

library(tidyverse)
dfd3 <-  dfD %>% 
 mutate(L3 = round(Dval, digits = 3))

dfd3
R> dfD
     D     Dval     L3
1  D25 20.90942 20.909
2  D53 21.51484 21.515
3  D59 18.11695 18.117
4  D44 20.29304 20.293
5  D57 19.51578 19.516
6  D22 21.72018 21.720
7  D16 20.62322 20.623
8  D31 21.97484 21.975
9  D18 19.26036 19.260
10 D61 22.08744 22.087
11 D37 19.36332 19.363
12 D55 20.67799 20.678

I think that's what you're looking for.

Dharman
  • 30,962
  • 25
  • 85
  • 135
David T
  • 1,993
  • 10
  • 18