This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R (tidyr::pivot_wider)

Hi there, I just use tidyr::pivot_wider (https://tidyr.tidyverse.org/reference/pivot_wider.html) to get a matrix with following .csv:

>     Coordinates   Numbers Values
>     chr1:110238914-110324454  NumB    1
>     chr1:110238914-110324454  NumC    3
>     chr1:110238914-110324454  NumD    1
>     chr5:65562670-65627908    NumD    1
>     chr5:65562670-65627908    NumA    1
>     chr5:65562670-65627908    NumB    4
>     chr5:65562670-65627908    NumC    1
>     chr2:158248715-158335919  NumB    1
>     chr2:158248715-158335919  NumA    0
>     chr2:158248715-158335919  NumC    1

I have used the below codes:

data= read.csv("Example.csv", header = F)
d= read.csv("Example.csv", sep="\t", header=TRUE, stringsAsFactors=FALSE)
d= tidyr::pivot_wider(df, names_from="Numbers", values_from="Values", values_fill = 2)

I tried it and the platform could read the input file, but I got following error, previously:

Error in `chr_as_locations()`:
! Can't subset columns that don't exist.
x Column `station` doesn't exist.
Run `rlang::last_error()` to see where the error occurred.

I would very appreciate if there are some one to solve this problem!

pivot r data

Hosin please use the "Code sample" option to present your code. enter image description here

BTW, from what I can see you passed df to the pivot_wider() while you did not define that before.

please double check your code. Currently what you are showing doesn't make that much sense as rpolicastro has pointed out.

What happens when you try the following instead:

data <- read.csv("Example.csv", sep="\t", header=TRUE, stringsAsFactors=FALSE)
df <- tidyr::pivot_wider(data, names_from="Numbers", values_from="Values", values_fill = 2)

Thanks **I have tried the above codes that you mentioned,

data <- read.csv("Example.csv", sep="\t", header=TRUE, stringsAsFactors=FALSE)
df <- tidyr::pivot_wider(data, names_from="Numbers", values_from="Values", values_fill = 2)

But unfortunately there is same error:**

Error in `chr_as_locations()`:
! Can't subset columns that don't exist.
x Column `Numbers` doesn't exist.
Run `rlang::last_error()` to see where the error occurred

Can you show what is the result for :

colnames(data)

Actually I want to make a matrix by the above file. Whereas coordinates to be as row names and Nums as column name, then if the coordinate has related Num put the related value in the matrix, if the coordinate does not the value for the sample just put 2 in the matrix, the result should be like below.

Coordinates   NumA    NumB    NumC    NumD
chr1:110238914-110324454  2   1   3   1
chr5:65562670-65627908    1   4   1   1
chr2:158248715-158335919  0   1   1   2

I see, still, it would be helpful to see what are the column names of the data dataframe. Indeed the error is self-explanatory and it is complaining about the column with "Numbers" as a column name. R believes you don't have "Numbers" in column names.

0 answers

No answers yet.

Log in to answer this question.