convert data frame with character column to data frame with integer column
1
0
Entering edit mode
13 months ago

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 • 1.5k views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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?

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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 :)

ADD REPLY
0
Entering edit mode
ADD COMMENT

Login before adding your answer.

Traffic: 2639 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6