R (tidyr::pivot_wider)
0
0
Entering edit mode
23 months ago
hosin • 0

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

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)
ADD REPLY
0
Entering edit mode

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
ADD REPLY
0
Entering edit mode

Can you show what is the result for :

colnames(data)
ADD REPLY
0
Entering edit mode

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
ADD REPLY
1
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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.

ADD REPLY

Login before adding your answer.

Traffic: 2565 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