Build Data Frame with "get"

5

This is my data.frame:

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 a data.frame ("NEW") for each country (A, B, and C) with the columns: P.A , P.D , i.A , i.D with%. That is, my reference will always be country D. I would like to do this with the "get" function but I can not do it. Any 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)))
  }

Any help?

    
asked by anonymous 09.05.2016 / 00:17

1 answer

4

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

To get more than one object you would have to use the mget function, but even then it does not make much sense in this case, since you can select columns by name in data.frame .

One way to solve your problem would be as follows:

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 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 to the global environment (which I would not recommend, it's much easier to work with them in the list), just run list2env(lista_de_dfs, globalenv()) .

Just to illustrate, if you want to do using mget() - which do not recommend , as well as 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 is no reason to do this instead of working with the natural selection of data.frames .

    
09.05.2016 / 16:35