making the first row a header in a dataframe in r -
i've seen asked here: create header of dataframe first row in data frame
and here: assign headers based on existing row in dataframe in r
and solutions offered don't work me.
when transpose dataframe (p1), header of df.transpose (p1t) new , annoying. , first row of p1t use header, tried:
colnames(p1t) = p1t[1, ] and doesn't work!
here how original df appears:
file fp1.pd_shortsoa_fam fp1.pd_longsoa_fam fp1.pd_shortsoa_semplus_real fp1.pd_shortsoa_semplus_fict sub0001 0,446222 2,524,804 0,272959 1,281,349 sub0002 1,032,688 2,671,048 1,033,278 1,217,817 and here how transpose appears:
row.names v1 v2 file sub0001 sub0002 fp1.pd_shortsoa_fam 0,446222 1,032,688 fp1.pd_longsoa_fam 2,524,804 2,671,048 fp1.pd_shortsoa_semplus_real 0,272959 1,033,278 fp1.pd_shortsoa_semplus_fict 1,281,349 1,217,817 fp1.pd_shortsoa_semminus_real 0,142739 1,405,100 fp1.pd_shortsoa_semminus_fict 1,515,577 -1,990,458 how can make "file", "sub0001","sub0002" etc... header?
thanks!
works me (with little trick).
x <- read.table(text = "file fp1.pd_shortsoa_fam fp1.pd_longsoa_fam fp1.pd_shortsoa_semplus_real fp1.pd_shortsoa_semplus_fict sub0001 0,446222 2,524,804 0,272959 1,281,349 sub0002 1,032,688 2,671,048 1,033,278 1,217,817", header = true) x <- t(x) colnames(x) <- x[1, ] x <- x[-1, ] x sub0001 sub0002 fp1.pd_shortsoa_fam "0,446222" "1,032,688" fp1.pd_longsoa_fam "2,524,804" "2,671,048" fp1.pd_shortsoa_semplus_real "0,272959" "1,033,278" fp1.pd_shortsoa_semplus_fict "1,281,349" "1,217,817"
Comments
Post a Comment