Reputation: 15
I have multiple dataframes in a list, with each dataframe representing a different time series location.
Each station dataframe element is currently within a list in a similar format to the following:
data.frame(Station1_1 = c(1:365),DayOfYear = 1:365, Temperature = c(rnorm(1:365, mean=10, sd=2)))
data.frame(Station2_1 = c(1:365),DayOfYear = 1:365, Temperature = c(rnorm(1:365, mean=10, sd=2)))
I plan to merge the dataframes together, but want to preserve the station number information (Station1 etc.) for each dataframe. Therefore I would like to rename each row in column 1 to the column name so that I don't have to enter the individual station names, and the script will take that information from the dataframe itself.
An example of the desired output would be:
data.frame(Station1_1 = c("Station1"),DayOfYear = 1:365,Temperature = c(rnorm(1:365, mean=10, sd=2)) )
A bonus would be if the "_1" could also be removed from the end of each name?
I'm not sure if a looping function would be best or if there is some other base R function to do this?
Upvotes: 0
Views: 143
Reputation: 39595
Try this:
#Data
df <- data.frame(Station1_1 = c(1:365),DayOfYear = 1:365, Temperature = c(rnorm(1:365, mean=10, sd=2)))
#Code
df$Station1_1 <- sub("_[^_]+$", "", names(df)[1])
Output (some rows):
head(df)
Station1_1 DayOfYear Temperature
1 Station1 1 11.819052
2 Station1 2 11.272291
3 Station1 3 9.711139
4 Station1 4 13.338525
5 Station1 5 9.276242
6 Station1 6 8.631387
Upvotes: 1