[R] Extract
Bert Gunter
bgunter@4567 @end|ng |rom gm@||@com
Sun Jul 21 20:19:48 CEST 2024
Nice! -- Let read.table do the work of handling the NA's.
However, even simpler is to use the 'colnames' argument of
read.table() for the column names no?
string <- read.table(text = dat$string, fill = TRUE, header =
FALSE, na.strings = "",
col.names = paste0("s", 1:5))
dat <- cbind(dat, string)
-- Bert
On Sun, Jul 21, 2024 at 10:16 AM Gabor Grothendieck
<ggrothendieck using gmail.com> wrote:
>
> We can use read.table for a base R solution
>
> string <- read.table(text = dat$string, fill = TRUE, header = FALSE,
> na.strings = "")
> names(string) <- paste0("S", seq_along(string))
> cbind(dat[-3], string)
>
> On Fri, Jul 19, 2024 at 12:52 PM Val <valkremk using gmail.com> wrote:
> >
> > Hi All,
> >
> > I want to extract new variables from a string and add it to the dataframe.
> > Sample data is csv file.
> >
> > dat<-read.csv(text="Year, Sex,string
> > 2002,F,15 xc Ab
> > 2003,F,14
> > 2004,M,18 xb 25 35 21
> > 2005,M,13 25
> > 2006,M,14 ac 256 AV 35
> > 2007,F,11",header=TRUE)
> >
> > The string column has a maximum of five variables. Some rows have all
> > and others may not have all the five variables. If missing then fill
> > it with NA,
> > Desired result is shown below,
> >
> >
> > Year,Sex,string, S1, S2, S3 S4,S5
> > 2002,F,15 xc Ab, 15,xc,Ab, NA, NA
> > 2003,F,14, 14,NA,NA,NA,NA
> > 2004,M,18 xb 25 35 21,18, xb, 25, 35, 21
> > 2005,M,13 25,13, 25,NA,NA,NA
> > 2006,M,14 ac 256 AV 35, 14, ac, 256, AV, 35
> > 2007,F,11, 11,NA,NA,NA,NA
> >
> > Any help?
> > Thank you in advance.
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
>
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
More information about the R-help
mailing list