Reputation: 23
Suppose I have a dataset:
test = data.frame(x=c(1:11), y=as.character(c(1:11)))
where the collumn 'y' is made up of characters/strings. Now, I would like to change the strings containing two characters (i.e. test[10,2] & test[11,2]) so that those strings start with a character "0". The result of this would be "010" & "011", whereas the other strings (with only one character) remain the same. To me, the logical solution would be:
test[nchar(test[,2])==2,2] = paste(c("0", test[nchar(test[,2])==2,2]), collapse="")
Indeed, only test[10,2] & test[11,2] are affected. The odd thing is though, that the result is test[10,2] = "01011", and test[11,2] = "01011". This means that all strings having two characters are pasted together with a preceding "0". This definitely is not what I would like to see.
What should I do to add only one character to a string in a dataset when certain conditions (of length) are met? Your answer would be greatly appreciated.
Upvotes: 2
Views: 2885
Reputation: 66834
Perhaps it would be more readable using ifelse
:
transform(test,y=ifelse(nchar(y)==2,paste0("0",y),y))
x y
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 010
11 11 011
Upvotes: 3
Reputation: 1248
I would do the same thing as ttmaccer suggests, except that I would use paste0, which automatically concatenates them without any spaces. It's a bit more efficient than paste, so it can be worth doing if you have a large dataset. It means you don't have to remember the 'sep' argument either.
test[nchar(test[,2])==2,2] = paste0("0", test[nchar(test[,2])==2,2])
Upvotes: 2
Reputation: 7475
use
paste("0", test[nchar(test[,2])==2,2], sep="")
so for example
> test[nchar(test[,2])==2,2] = paste("0", test[nchar(test[,2])==2,2], sep="")
> test
x y
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 010
11 11 011
collapse
and sep
have different properties
> paste(1,c(2:3),collapse=',')
[1] "1 2,1 3"
> paste(1,c(2:3),sep=',')
[1] "1,2" "1,3"
Upvotes: 4