Home > Enterprise >  How to create datatable with columnname , datatypes and few data in R?
How to create datatable with columnname , datatypes and few data in R?

Time:01-30

I was wondering if I can create DataTable containing 4 columns. "Column Name","Data Type" "top 5" and "last5" data ? Basically, converting str(df) output into datatable. Not sure if there is package available for this task. If there is please point to that package.

Thanks in advance for your time and efforts!

CodePudding user response:

Something like this might help:

Let's say your data looks like this

data = data.frame(
  x1 = sample(letters,20),
  x2 = sample(1:100,20)
)

Then, you can create a DataTable with your five columns like this:

data.table(
  ColumnName = colnames(data),
  DataType = sapply(data,typeof),
  first5 = lapply(data,head,5),
  last5 = lapply(data,tail,5)
)

Output:

   ColumnName  DataType         first5          last5
1:         x1 character      x,w,p,c,n      h,k,z,t,f
2:         x2   integer 24,26,20,72, 1 45,25,18,80,54
  •  Tags:  
  • Related