This is a test version of Biostars. For the public version, visit https://www.biostars.org.
convert data frame with character column to data frame with integer column

I have a data frame that shows :

"dataframe1" :

rows : index

columns:

column 1: a gene name

column 2: mutation

column 3: a sample

example

What I would like to achieve is below structure:

"dataframe2":

rows: a gene name

columns:

sample 1: if a mutation occurred for sample1,then put the number1; Otherwise put the number zero.

sample 2: if a mutation occurred for sample2,then put the number1; Otherwise put the number zero.

sample 3: if a mutation occurred for sample3,then put the number1; Otherwise put the number zero. . . .

exaple

How can I convert "dataframe1" to "dataframe2" ? could you guide me please ?

r

You would better give an example with values because we don't know what is in each variable so we cannot give you a precise code, but seems you need something like df1 %>% pivot_wider(names_from=sample,values_from = mutation)using tidyverse

Thank you so much for your guidance. I tried the code that you have suggested but I got this error :

Error in pivot_wider(): ! Can't convert fill<double> to <list>

could you help me to resolve this error please?

Sorry, as I indicated in my comment, we do not know what does your dataframe look like, I cannot help further without a reproducible example

dear @Basti : As you said in order to clarify my issue I edit question box with give an example. I will be grateful if you take a look at it and if it possible help me to resolve my problem please. Thanks a lot :)

1 answer

Log in to answer this question.