[R] Re : paste first row string onto every string in column
Gavin Simpson
gavin.simpson at ucl.ac.uk
Wed Aug 12 12:13:38 CEST 2009
On Wed, 2009-08-12 at 09:52 +0000, Inchallah Yarab wrote:
>
> try this , save your data in C:/
> and write this
> (data <- read.csv2("c:/jill.csv", sep=","))
Why use read.csv2 yet change the separator to be the decimal point
indicator in that function. You are really asking for trouble reading
data that way.
Use read.csv(), or read.csv2() if in a locale where the decimal point is
",", as convenient wrappers and if you need to change the defaults, use
read.table() directly instead.
> (C<-data[1,])
You really shouldn't call your data, 'data' as it will get very
confusing when you start using modelling functions and using the data
argument, or using inbuilt data sets and thus using the data() function.
For a funny aside on this, do the following:
> install.packages("fortunes")
> require("fortunes")
Loading required package: fortunes
> fortune("dog")
Firstly, don't call your matrix 'matrix'. Would you call your dog 'dog'?
Anyway, it might clash with the function 'matrix'.
-- Barry Rowlingson
R-help (October 2004)
> A <- numeric(4)
> B <- numeric(4)
> for (i in 1 :4){
> A[i] <- paste(data[1,i],data[2,i])
> B[i] <- paste(data[1,i],data[3,i])
> }
paste is vectorised, so you don't need the loop here. This should work
A <- paste(data[1, ], data[2, ])
B <- paste(data[1, ], data[3, ])
without the loop.
HTH
G
> A
> B
> (data1 <- rbind(as.character(A),as.character(B)))
> (data2 <- rbind(C,data1))
>
> normaly that gives that
> > (data <- read.csv2("c:/jill.csv", sep=","))
> V1 V2 V3 V4
> 1 DPA1* DPA1* DPB1* DPB1*
> 2 103 104 401 601
> 3 103 103 301 402
> > (C<-data[1,])
> V1 V2 V3 V4
> 1 DPA1* DPA1* DPB1* DPB1*
> > A <- numeric(4)
> > B <- numeric(4)
> > for (i in 1 :4){
> + A[i] <- paste(data[1,i],data[2,i])
> + B[i] <- paste(data[1,i],data[3,i])
> + }
> > A
> [1] "DPA1* 103" "DPA1* 104" "DPB1* 401" "DPB1* 601"
> > B
> [1] "DPA1* 103" "DPA1* 103" "DPB1* 301" "DPB1* 402"
> >
> > (data1 <- rbind(as.character(A),as.character(B)))
> [,1] [,2] [,3] [,4]
> [1,] "DPA1* 103" "DPA1* 104" "DPB1* 401" "DPB1* 601"
> [2,] "DPA1* 103" "DPA1* 103" "DPB1* 301" "DPB1* 402"
> > (data2 <- rbind(C,data1))
> V1 V2 V3 V4
> 1 DPA1* DPA1* DPB1* DPB1*
> 2 DPA1* 103 DPA1* 104 DPB1* 401 DPB1* 601
> 3 DPA1* 103 DPA1* 103 DPB1* 301 DPB1* 402
> >
>
> Hope that helps!!!
>
> inchallah yarab
>
>
> ________________________________
> De : Jill Hollenbach <jhollenbach at chori.org>
> : r-help at r-project.org
> Envoy le : Mercredi, 12 Aot 2009, 3h48mn 37s
> Objet: [R] paste first row string onto every string in column
>
>
> Hi,
> I am trying to edit a data frame such that the string in the first line is
> appended onto the beginning of each element in the subsequent rows. The data
> looks like this:
>
> > df
> V1 V2 V3 V4
> 1 DPA1* DPA1* DPB1* DPB1*
> 2 0103 0104 0401 0601
> 3 0103 0103 0301 0402
> .
> .
> and what I want is this:
>
> >dfnew
> V1 V2 V3 V4
> 1 DPA1* DPA1* DPB1* DPB1*
> 2 DPA1*0103 DPA1*0104 DPB1*0401 DPB1*0601
> 3 DPA1*0103 DPA1*0103 DPB1*0301 DPB1*0402
>
> any help is much appreciated, I am new to this and struggling.
> Jill
>
> ___
> Jill Hollenbach, PhD, MPH
> Assistant Staff Scientist
> Center for Genetics
> Children's Hospital Oakland Research Institute
> jhollenbach at chori.org
>
> ______________________________________________
> 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.
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Dr. Gavin Simpson [t] +44 (0)20 7679 0522
ECRC, UCL Geography, [f] +44 (0)20 7679 0565
Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
More information about the R-help
mailing list