Entering edit mode
2.5 years 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!
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:
Thanks **I have tried the above codes that you mentioned,
But unfortunately there is same error:**
Can you show what is the result for :
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.
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.Hosin please use the "Code sample" option to present your code.
BTW, from what I can see you passed
df
to thepivot_wider()
while you did not define that before.