[R] Build a data.table via two string lists

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Tue Jun 7 02:22:31 CEST 2022


Maybe I don't quite understand, but to get a 1 row data table the cl has to be column names.

colnames(df) <- cl


Tim

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Bill Dunlap
Sent: Monday, June 6, 2022 1:08 PM
To: John RF Staples <john.staples using me.com>
Cc: r-help using R-project.org
Subject: Re: [R] Build a data.table via two string lists

[External Email]

You could turn your row values in a list, attach names to that list, and pass the list to data.frame():
> cl <- c("timestamp", "assess", "catheter", "service", "ref") rw <- 
> c("1654508112", "3", "gel", "2785",
"16545081121a8d8f956cb871053a3f56b782b70a76")
> df <- data.frame(setNames(as.list(rw), nm=cl)) df
   timestamp assess catheter service
 ref
1 1654508112      3      gel    2785
16545081121a8d8f956cb871053a3f56b782b70a76

At this point all the columns have type "character".  You may want to convert some columns to more meaningful types, e.g., "timestamp" to a date/time class and "assess" to a number.  E.g.,
> df[["timestamp"]] <- as.POSIXct(as.numeric(df[["timestamp"]]),
origin=as.POSIXct("1970/01/01"))
> df[["assess"]] <- as.numeric(df[["assess"]]) df
            timestamp assess catheter service
         ref
1 2022-06-06 10:35:12      3      gel    2785
16545081121a8d8f956cb871053a3f56b782b70a76
> str(df)
'data.frame':   1 obs. of  5 variables:
 $ timestamp: POSIXct, format: "2022-06-06 10:35:12"
 $ assess   : num 3
 $ catheter : chr "gel"
 $ service  : chr "2785"
 $ ref      : chr "16545081121a8d8f956cb871053a3f56b782b70a76"

-Bill


On Mon, Jun 6, 2022 at 9:51 AM John RF Staples via R-help < r-help using r-project.org> wrote:

> Hello everyone,
>
> I have the following two structures:
>
> # str(cl)
> # chr [1:5] "timestamp" "assess" "catheter" "service" "ref"
>
> # str(rw)
> # chr [1:5] "1654508112" "3" "gel" "2785"
> “16545081121a8d8f956cb871053a3f56b782b70a76"
>
> From which I would like to generate a 5 col x 1 row data.table; the 
> question is ‘how’?
>
> ‘cl’ represents the column names; ‘rw’ the row values.
>
> With anticipatory thanks …
> John
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see 
> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mail
> man_listinfo_r-2Dhelp&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAs
> Rzsn7AkP-g&m=K1a_rA-Z0ef6qHvn7vTcCIiwqM3-TiLHpEkHxPn9eW2bhsutM9gsG6l7Z
> IygZrrc&s=Kv3c5xefTDI4vqOIrrz8s6kS-9Yt30ttEFpzh6uMVq8&e=
> PLEASE do read the posting guide
> https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.or
> g_posting-2Dguide.html&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeA
> sRzsn7AkP-g&m=K1a_rA-Z0ef6qHvn7vTcCIiwqM3-TiLHpEkHxPn9eW2bhsutM9gsG6l7
> ZIygZrrc&s=iXx9A0KYXW8fUfuPHkg2NpKITg-FGyQuYTU5vSA4zOo&e=
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=K1a_rA-Z0ef6qHvn7vTcCIiwqM3-TiLHpEkHxPn9eW2bhsutM9gsG6l7ZIygZrrc&s=Kv3c5xefTDI4vqOIrrz8s6kS-9Yt30ttEFpzh6uMVq8&e=
PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=K1a_rA-Z0ef6qHvn7vTcCIiwqM3-TiLHpEkHxPn9eW2bhsutM9gsG6l7ZIygZrrc&s=iXx9A0KYXW8fUfuPHkg2NpKITg-FGyQuYTU5vSA4zOo&e=
and provide commented, minimal, self-contained, reproducible code.


More information about the R-help mailing list