[R] a question to transform a dataframe empty 0/1

Arnaud Michel michel.arnaud at cirad.fr
Fri Jun 21 06:10:20 CEST 2013


Thank you arun !
I don't know the library reshape2
Michel

Le 20/06/2013 19:55, arun a écrit :
> Hi,
>
> Not sure if you wanted the entries with "0".
>
> library(reshape2)
>   dfMelt<-melt(df,id.var=c("Country","Iso"))
>
> #subset those with "1"
>
> dfNew<- subset(dfMelt,value==1,select=-4) row.names(dfNew)<- 1:nrow(dfNew)
>   dfNew
> #        Country Iso   variable
> #1      Zimbabwe  ZW      Abaco
> #2  South Africa  ZA      Abaco
> #3    Madagascar  MG      Abaco
> #4          Mali  ML      Abaco
> #5         Kenya  KE      Abaco
> #6  Burkina Faso  BF      Abaco
> #7      Tanzania  TZ Adaptclone
> #8          Mali  ML Adaptclone
> #9    Mozambique  MZ Adaptclone
> #10   Madagascar  MG Adaptclone
> #11        Ghana  GH Adaptclone
> #12      Nigeria  NG Adaptclone
>
>
> A.K.
>
>
> Hello
>
> I have the following dataframe :
> df <- data.frame(
> Country=c("Zimbabwe","Burkina Faso","South Africa","Madagascar","Tanzania",
> "Mali","Mozambique","Madagascar","Ghana","Nigeria","Kenya","Burkina Faso",
>    "South Africa","Tanzania","Kenya","Ethiopia" ) ,
>
> Iso=c("ZW","BF","ZA","MG","TZ","ML","MZ","MG","GH","NG","KE","BF",
>    "ZA","TZ","KE","ET") ,
>
> Abaco=c(1,0,1,1,0,1,0,0,0,0,1,1,0,0,0,0) ,
> Adaptclone= c(0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0)
> )
> There is a lot of column like Abaco, Adaptclone,...
> I would like to built a dataframe
> wich transforms the initial dataframe of 4 columns into a dataframe of 3
> columns as the following dataframe
>
> Country          Iso    Project
> Zimbabwe         ZW     Abaco
> South Africa     ZA     Abaco
> Madagascar       MG     Abaco
> Tanzania         TZ     Adaptclone
> Mali             ML     Abaco
> Mali             ML     Adaptclone
> ......
>
> Any idea ?
> Michel
>

-- 
Michel ARNAUD
Chargé de mission auprès du DRH
DGDRD-Drh - TA 174/04
Av Agropolis 34398 Montpellier cedex 5
tel : 04.67.61.75.38
fax : 04.67.61.57.87
port: 06.47.43.55.31



More information about the R-help mailing list