[R] concatenate 2 column vectors
David Winsemius
dwinsemius at comcast.net
Thu Jan 21 05:23:28 CET 2010
On Jan 20, 2010, at 11:14 PM, teurlai magali wrote:
> Sorry I forgot the subject in my previous post
>
> Here is an example of data : province and district code of 4 locations
>
> datatry=matrix(c(8,12,3,3,1,1,16,1),4,2)
> colnames(datatry)<-c("PROCODE","DISCODE")
> cbind( datatry, paste(datatry[, "PROCODE"], datatry[, "DISCODE"],
sep=".") )
PROCODE DISCODE
[1,] "8" "1" "8.1"
[2,] "12" "1" "12.1"
[3,] "3" "16" "3.16"
[4,] "3" "1" "3.1"
>
> DISCODE is not unique for each location (there is a distrcit nb 1 in
> each province)
> I want to create a third column (DIS) which would contain a unique
> district code for each location that would be the appended values of
> the province code and the district code, and that would be a numeric
> value (not a character)
>
> here is my try (thanks to Henrique Dallazuanna!)
>
> DIS=rep(0,nrow(datatry))
> datatry=cbind(datatry,DIS)
> datatry2=transform(datatry,DIS=paste(datatry[,1],datatry[,2]));
>
> However it creates a string, and the 2 codes are separated by an
> empty space, which I didn't manage to supress using different values
> for arguments "sep" and "collapse".
>
> i'll try to see other options to concatenate 2 numeric values into a
> third one.
>
> Thanks
>
>
> --- En date de : Jeu 21.1.10, Henrique Dallazuanna
> <wwwhsd at gmail.com> a écrit :
>
> De: Henrique Dallazuanna <wwwhsd at gmail.com>
> Objet: Re: [R] (no subject)
> À: "teurlai magali" <teurlaimag at yahoo.fr>
> Cc: r-help at r-project.org
> Date: Jeudi 21 Janvier 2010, 3h41
>
> transform(DF, District_Unique = paste(Province, District, collapse =
> ''))
>
> On Thu, Jan 21, 2010 at 12:34 AM, teurlai magali
> <teurlaimag at yahoo.fr> wrote:
>> Hi
>>
>> is there a way in R to create a new column vector with the numbers
>> of 2 others columns simply appended?
>>
>> Example : I have a column with provinces codes (1 to 19)
>> I have a second column with districts codes (1 to ##, depending on
>> the province)
>> I want to create a third column with a code that would be unique
>> for each district:
>>
>> Province District District_Unique
>> 1 1 11
>> 1 2 12
>> 13 2 132
>>
>> I can create a loop, but it becomes complicated with numerous data
>>
>> Thanks for your help
>>
>>
>>
>>
>>
>> [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list