Reputation: 1581
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)
Upvotes: 2
Views: 2537
Reputation: 93813
A variation using Map
and some "[<-"
trickery:
vars <- ls(pattern="water_.")
l <- mget(vars)
names(l) <- substr(vars,nchar(vars)-3,nchar(vars))
do.call(rbind,Map("[<-",l,TRUE,"park",names(l)))
Upvotes: 0
Reputation: 121568
You can do this for example:
do.call(rbind,lapply(ls(pattern='water.*'),
function(x) {
dat=get(x)
dat$park = sub('.*_(.*)$','\\1',x)
dat
}))
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.sub
will extract the last part of the namedo.call
+ rbind
applied to the resulted list to get a unique big data.frameusing 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
Upvotes: 6
Reputation: 70623
I would put the data.frames in a named list and perform this task:
rslt <- list(water_land_by_ownname_apis = water_land_by_ownname_apis,
water_land_by_ownname_indu = water_land_by_ownname_indu)
for (i in names(rslt)) {
col <- unlist(strsplit(i, "_"))[5]
rslt[[i]]$park <- col
}
do.call("rbind", rslt)
Upvotes: 2