Reputation: 1147
G'day Everyone,
I am really sorry I am coming here for this problem but I am missing something very basic here. I have downloaded the ascii file from here. I have sub-setted this data to include only the columns and rows I am interested in. Now all I want to do is replace the short 'state' names with long-form state names. I need the long-form state names to feed into another function I have written.
If you can't be bother downloading the file I'll try to roughly reproduce the data.frame.
# If data file was downloaded.
gazet <- read.table('file location', sep = ',')
gazet <- gazet[,c(2:6, 8, 10, 14)]
colnames(gazet) <- c('recID', 'authID', 'state', 'name', 'feature',
'postcode', 'lon', 'lat')
towns <- gazet[which(gazet$feature %in% c('LOCB', 'SUB', 'URBN', 'POPL')
& !gazet$state %in% c('JBT', 'NFK', 'MCD', 'HRD', 'AAT', 'N/A')),]
# If you don't want to download the file.
towns <- data.frame('recID' = c('G62', 'G63', 'G64'),
'state' = c('QLD', 'SA', 'VIC'),
'name' = c('Balgal Beach', 'Balhannah', 'Ballan'),
'feature' = c('POPL', 'POPL', 'POPL'))
# Using whichever towns you prefer, first I change state to character
# so that it can be changed.
towns$state <- as.character(towns$state)
state_longform <- function(x){
for (i in 1:length(x$state)){
if (x[i, 'state'] == 'VIC'){
x[i, 'state'] <- 'Victoria'
} else{ x[i, 'state'] == 'NA'}
}
}
state_longform(towns)
head(towns)
My function doesn't work, yet if I do each component outside of the function it does.
towns[1, 'state'] == 'VIC'
towns[3, 'state'] == 'VIC'
towns[3, 'state'] <- 'Victoria'
I plan for the full function will change all of the state names to their longform (e.g. 'QLD' to 'Queensland', etc), but I started with a shorter function to make sure it works. Which it doesn't :-(.
I am sure there is something really stupid that I am missing and I am really sorry but I haven't gotten anywhere in a long time. Is stackoverflow the right place for questions when you know you are being an idiot or is there a better place for this sort of simple question?
Thanks lots for your help.
Regards, Adam
Upvotes: 0
Views: 74
Reputation: 32416
Could you just do this instead?
towns[towns$state == "VIC", ]$state <- "Victoria"
If you want to swap ALL the short names with long names do something like this, but make sure that index of corresponding long and short names are the same in the longs
and shorts
vectors:
longs <- c("Victoria", "Queensland", "New South Wales", "South Australia")
shorts <- c("VIC", "QLD", "NSW", "SA")
towns$state <- longs[match(towns$state, shorts)]
> head(towns)
recID authID state name feature postcode lon
33704 G62 GA Queensland Balgal Beach POPL 4816 146.4083
33705 G63 GA South Australia Balhannah POPL 5242 138.8257
33706 G64 GA Victoria Ballan POPL 3342 144.2289
33707 G65 GA Victoria Ballarat POPL 3353 143.8626
33708 G66 GA New South Wales Ballina POPL 2478 153.5654
33709 G67 GA New South Wales Balranald POPL 2715 143.5633
Upvotes: 1
Reputation: 7582
You need to return the changed values from the function.
state_longform <- function(x){
for (i in 1:length(x$state)){
if (x[i, 'state'] == 'VIC'){
x[i, 'state'] <- 'Victoria'
} else{ x[i, 'state'] == 'NA'}
}
return(x)
}
You could also replace return(x)
with just x
on that line and the function would return the value, but I think explicate returns make the code more readable.
Upvotes: 2