简体   繁体   中英

How to get dataframe value?

I have a dataframe df. I took the column headers in another dataframe as I want to run a loop on it. This is the output of the header dataframe

df.header
#  [1] ISBAD            MA_KH            MA_CN            Ngay.xep.hang    So.ho.so         SEQ              Primary_Key      Nganh            Mo.hinh.xep.hang
# [10] Loai.hinh.DN     NAM_TAI_CHINH    Liq1             Liq4             Liq5             Per1             Per2             Per3             Per4            
# [19] Per5             Lev1             Lev2             Lev3             Lev4             Lev5             Prof1            Prof2            Prof3           
# [28] Prof4            Prof5            Gro14            Gro15            Gro16            Gro17            Gro18            Gro19            Gro20           
# [37] Struc1           Cov1             Liq6             Prof6            Struc2           Lev6             Lev7             Lev8             Struc3          
# [46] Struc4           Struc5           Prof8            Struc6           Liq7             Lev9             Cov.24           Cov2             Liq9            
# [55] Cov4             Prof9            Struc7           Cov6             Prof10           Prof13           Prof16           Prof18           Prof19          
# [64] Prof22           Per6             Per7             Per8             Prof23           Cov7             Prof24           Lev10            Struc8          
# [73] Struc9           Lev11            Struc10          Liq10            cov3             Cov9             Cov10            Liq11            Cov11           
# [82] Prof29           Prof30           Per9             Per10            Liq12            Cov12            Cov13            Liq13            Cov15           
# [91] Per11            Per12            Per13            Per14            Cov16            Cov17            Cov18            Gro21            Gro22           
#[100] Cov19            Cov20            Liq14            Liq15            Liq16            Liq17            Liq18            Struc11          Struc15         
#[109] Liq19            Prof34           Prof35           Prof38           Prof40           Prof41           Prof42           Prof43           Struc12         
#[118] Struc13          Struc14          Cov21            Cov22            Prof44           Gro23            Liq20            Cov23            Liq21           
#[127] Liq22            Lev12            Prof31   

Now when I put in the following code in the loop

liststring <- toString(df.header[2])

I got the output

liststring
# [1] "integer(0)"

Instead of MA_KH

I also tried toString(df.header[2],) and got the same result.

Not sure where I'm going wrong here

With command df.header <-head(df,0) you don't get a dataframe of colum header, but an empty copy of your original dataframe. to get get just the names of a dataframe use: names(df).

Maybe you can post the purpose of this new dataframe. Iterating over variables of a dataframe can be done using lapply without creating a new dataframe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM