Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

r function/loop to add column and value to multiple dataframes

I have 8 data frames that I want to add a column called 'park', then fill this column in w/ a value that comes from the last four characters of the dataframe name. Here are two of my eight data frames:

water_land_by_ownname_apis <- structure(list(OWNERNAME = c("Forest Service (USFS)", "Fish and Wildlife Service (FWS)", 
"State Department of Natural Resources", "Private Landowner", 
"National Park Service (NPS)", "Unknown", "Private Institution", 
"Native American Land"), WATER = c(696600, 9900, 1758600, 26100, 
112636800, 1586688300, 0, 11354400), LAND = c(258642900, 997200, 
41905800, 2536200, 165591900, 1075917600, 461700, 314052300)), class = "data.frame", .Names = c("OWNERNAME", 
"WATER", "LAND"), data_types = c("C", "F", "F"), row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8"))

water_land_by_ownname_indu <- structure(list(OWNERNAME = c("The Nature Conservancy (TNC)", 
"Other State Land", "Private Institution", "State Department of Transportation", 
"State Department of Natural Resources", "Unknown", "National Park Service (NPS)", 
"Private Landowner", "Joint Ownership", "Private Non-profit", 
"Land Trust"), WATER = c(24300, 1018800, 5282100, 0, 12600, 19192500, 
802800, 139500, 0, 0, 0), LAND = c(719100, 10045800, 12556800, 
900, 2018700, 1446426000, 42484500, 5769900, 38700, 852300, 70200
)), class = "data.frame", .Names = c("OWNERNAME", "WATER", "LAND"
), data_types = c("C", "F", "F"), row.names = c("1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11"))

Which look like this...

> water_land_by_ownname_apis
                              OWNERNAME      WATER       LAND
1                 Forest Service (USFS)     696600  258642900
2       Fish and Wildlife Service (FWS)       9900     997200
3 State Department of Natural Resources    1758600   41905800
4                     Private Landowner      26100    2536200
5           National Park Service (NPS)  112636800  165591900
6                               Unknown 1586688300 1075917600
7                   Private Institution          0     461700
8                  Native American Land   11354400  314052300
> water_land_by_ownname_indu
                               OWNERNAME    WATER       LAND
1           The Nature Conservancy (TNC)    24300     719100
2                       Other State Land  1018800   10045800
3                    Private Institution  5282100   12556800
4     State Department of Transportation        0        900
5  State Department of Natural Resources    12600    2018700
6                                Unknown 19192500 1446426000
7            National Park Service (NPS)   802800   42484500
8                      Private Landowner   139500    5769900
9                        Joint Ownership        0      38700
10                    Private Non-profit        0     852300
11                            Land Trust        0      70200

For each dataframe, I want to add a column ('park') and fill this column with the last four characters of the data frame name. For example...

water_land_by_ownname_apis$park <- 'apis'
water_land_by_ownname_indu$park <- 'indu'

Resulting in this...

> water_land_by_ownname_apis
                              OWNERNAME      WATER       LAND park
1                 Forest Service (USFS)     696600  258642900 apis
2       Fish and Wildlife Service (FWS)       9900     997200 apis
3 State Department of Natural Resources    1758600   41905800 apis
4                     Private Landowner      26100    2536200 apis
5           National Park Service (NPS)  112636800  165591900 apis
6                               Unknown 1586688300 1075917600 apis
7                   Private Institution          0     461700 apis
8                  Native American Land   11354400  314052300 apis
> water_land_by_ownname_indu
                               OWNERNAME    WATER       LAND park
1           The Nature Conservancy (TNC)    24300     719100 indu
2                       Other State Land  1018800   10045800 indu
3                    Private Institution  5282100   12556800 indu
4     State Department of Transportation        0        900 indu
5  State Department of Natural Resources    12600    2018700 indu
6                                Unknown 19192500 1446426000 indu
7            National Park Service (NPS)   802800   42484500 indu
8                      Private Landowner   139500    5769900 indu
9                        Joint Ownership        0      38700 indu
10                    Private Non-profit        0     852300 indu
11                            Land Trust        0      70200 indu

Then, rbind them together....

water_land_by_ownname <- rbind (water_land_by_ownname_apis, water_land_by_ownname_indu)

Then, remove prior data frames from memory...

rm (water_land_by_ownname_apis,water_land_by_ownname_indu)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do this for example:

do.call(rbind,lapply(ls(pattern='water.*'),
       function(x) {
         dat=get(x)
         dat$park = sub('.*_(.*)$','\1',x)
         dat
       }))
  1. ls will extract all data.frames names having certain pattern, here I assume you data.frame begin with the word water. This will be store names in a list handy for lapply use.
  2. sub will extract the last part of the name
  3. do.call+ rbind applied to the resulted list to get a unique big data.frame

using your 2 data.frames I get :

                              OWNERNAME      WATER       LAND park
1                  Forest Service (USFS)     696600  258642900 apis
2        Fish and Wildlife Service (FWS)       9900     997200 apis
3  State Department of Natural Resources    1758600   41905800 apis
4                      Private Landowner      26100    2536200 apis
5            National Park Service (NPS)  112636800  165591900 apis
6                                Unknown 1586688300 1075917600 apis
7                    Private Institution          0     461700 apis
8                   Native American Land   11354400  314052300 apis
12          The Nature Conservancy (TNC)      24300     719100 indu
21                      Other State Land    1018800   10045800 indu
31                   Private Institution    5282100   12556800 indu
41    State Department of Transportation          0        900 indu
51 State Department of Natural Resources      12600    2018700 indu
61                               Unknown   19192500 1446426000 indu
71           National Park Service (NPS)     802800   42484500 indu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...