r - Converting a dataframe of label/values to a named numeric vector -
i trying convert dataframe labels/values named numeric vecotr. illustration have next dataframe
>df=data.frame(lab=c("a","b","c","d"),values=c(1,2,3,4)) > df lab values 1 1 2 b 2 3 c 3 4 d 4 so trying iterate or utilize function on info frame next
>v_needed=c("a"=1,"b"=2,"c"=3,"d"=4) > v_needed b c d 1 2 3 4 i tried convert factor didn't give desired output
>v_failure=factor(df$values,labels=df$lab)
you can utilize setnames function
v <- with(df, setnames(values, lab)) v # b c d # 1 2 3 4 r
No comments:
Post a Comment