Build Data Frame with "get" Function

Asked

Viewed 105 times

5

This is my date.:

data<-read.csv2("NewEXEMPL.csv",header=TRUE,sep=";")

head(data,5)
    DATE   P.A      i.A     S.A       w.A   b.A   P.B      i.B    S.B         w.B   b.B      P.C     i.C       S.C   w.C   b.C
1 jun/79 16.86 59.67768 12.3125 0.4291845 497.9 10.38 28.41693 23.000 0.000862813 16.86 59.67768 12.3125 0.4291845 497.9 10.38
2 jul/79 16.69 59.90459 12.2500 0.4177109 533.6 10.48 28.73513 24.250 0.000838926 16.69 59.90459 12.2500 0.4177109 533.6 10.48
3 ago/79 16.62 60.28277 12.0625 0.4046945 542.7 10.55 28.90646 29.500 0.000814996 16.62 60.28277 12.0625 0.4046945 542.7 10.55
4 set/79 16.90 60.43405 12.3125 0.4085802 533.3 10.54 29.17570 36.125 0.000821018 16.90 60.43405 12.3125 0.4085802 533.3 10.54
5 out/79 17.05 60.73660 12.0000 0.4301075 495.3 10.59 29.59179 39.125 0.000849257 17.05 60.73660 12.0000 0.4301075 495.3 10.59
       P.D     i.D          S.D      w.D        b.D
1 28.41693  12.400 42.404741100 12.00000  0.2405581
2 28.73513  12.980 42.781114500 12.37500  0.2379819
3 28.90646  13.220 43.241126500 11.06250  0.2279202
4 29.17570 495.300  8.700000000 26.27222 18.2500000
5 29.59179  39.125  0.000849257 10.76000 54.1561672

has about 400 lines.

In this code below I would like to have for each country (A, B and C) a data.frame ("NEW") with columns: P.A, P.D, i.A, i.D and S.A. That is, my reference will always be the country D. I would like to do this with the "get" function but I’m not getting it. Some help?

mylist<-c("A","B","C") # São os países
for (cno in 1:3){
  country<-mylist[cno]
NEW<-data.frame(get(paste("S,i",country,sep=".",data)))
  }

Some help?

1 answer

4


The function get() is not suitable for what you want to do. It only takes one object at a time.

To pick up more than one object you would have to use the function mget, but still doesn’t make much sense in this case, since you can select columns by name in a data.frame.

One way to solve your problem would be the following:

mylist<-c("A","B","C") 

colunas <- lapply(mylist, function(x) paste(c("P", "i", "S"), x, sep = "."))

lista_de_dfs <- list()

for(i in seq_along(colunas))
  lista_de_dfs[[mylist[[i]]]] <- data[c(colunas[[i]], "P.D", "i.D")]

And the object lista_de_dfs is a list with the data.frames that you want, for example:

lista_de_dfs[["A"]]
    P.A      i.A     S.A      P.D     i.D
1 16.86 59.67768 12.3125 28.41693  12.400
2 16.69 59.90459 12.2500 28.73513  12.980
3 16.62 60.28277 12.0625 28.90646  13.220
4 16.90 60.43405 12.3125 29.17570 495.300
5 17.05 60.73660 12.0000 29.59179  39.125

If you want to play these data.frames for the global environment (which I wouldn’t recommend, it’s much easier to work with them on the list), just run list2env(lista_de_dfs, globalenv()).

Just to illustrate, if you want to do using the mget() -- what do not recommend, because in addition to much more complicated is much slower --, the logic would be quite similar:

mylist<-c("A","B","C") 

colunas <- lapply(mylist, function(x) paste(c("P", "i", "S"), x, sep = "."))

lista_de_dfs <- list()

for(i in seq_along(colunas))
  lista_de_dfs[[mylist[[i]]]] <- data.frame(mget(c(colunas[[i]], "P.D", "i.D"), as.environment(data)))

There’s no reason to do it this way instead of working with the natural way of selecting data.frames.

  • Thanks, Carlos! You helped a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.