I have a dataframe. What I want to achieve is to have a column (representing months) coded on 2 characters (string of length 2) instead of an integer.
Here is a little sample of data :
wifi <- data.frame(replicate(2,8:12))
Which creates a dataframe like this :
X1 X2
1 8 8
2 9 9
3 10 10
4 11 11
5 12 12
I want to have something like this :
X1 X2
1 8 08
2 9 09
3 10 10
4 11 11
5 12 12
Here is the function I made :
A <- function(x) {
if(nchar(x)==1) {
return(paste0("0",x))
} else {
return(x)
}
}
which seems to work as intended ( A("9") == "09" and A("12") == "12").
I tried this
cbind(wifi[1], lapply(wifi[2], A) )
Here is the result I got, it seems like this function is applied one time for all elements and not for each element.
X1 X2
1 8 08
2 9 09
3 10 010
4 11 011
5 12 012
Warning message: In if (nchar(x) == 1) { : the condition has length > 1 and only the first element will be used
Someone know I could fix that ?