I want to merge precipitation data with a .csv df. I figured out, that it should work with the command "extract" from the raster package.
That's my precipitation data:
str(precipitation_raster_layer)
Formal class 'RasterLayer' [package "raster"] with 12 slots
..# file :Formal class '.RasterFile' [package "raster"] with 13 slots
Thats my climate-related Aid data:
str(AID)
'data.frame': 1050 obs. of 21 variables:
$ project_location_id : Factor w/ 1050 levels "P000501_2427123",..: 189 190 191 192 193 194 188 195 196 187 ...
$ precision_code : int 3 3 3 3 3 3 3 3 3 2 ...
$ latitude : num 6.45 6.74 6.47 5.66 6.6 ...
$ longitude : num -1.583 -3.044 -2.333 -0.39 0.467 ...
Using this command:
test <- extract(precipitation_raster_layer, AID[,3:4])
Error in UseMethod("extract_") : no applicable method for
'extract_' applied to an object of class "c('RasterLayer', 'Raster',
'BasicRaster')"
If I transform the .csv to a SpatialPointsDataFrame and try to run "extract", I get this error:
test <- extract(precipitation_raster_layer, AID_spatial_df)
Error in UseMethod("extract_") :
no applicable method for 'extract_' applied to an object of class "c('RasterLayer', 'Raster', 'BasicRaster')"
I really do not understand why it says that my object is not a RasterLayer.
Any help appreciated.
My guess is that you have, after loading raster, loaded another package that also has an extract method that hides the method from raster.
Load only the packages you need, and try calling the extract method from raster explicitly:
raster::extract(precipitation_raster_layer, AID[, 4:3])
Note that it should be AID[, 4:3], not AID[, 3:4], as the correct order is longitude, latitude. But that is not the cause of the error you are getting.
You might probably have loaded tidyverse, unload it and try again
Related
I have the following object
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
..# i : int [1:120671481] 0 2 3 6 10 13 21 22 25 36 ...
..# p : int [1:51366] 0 3024 4536 8694 3302271 3302649 5715381 5756541 5784009 5801691 ...
..# Dim : int [1:2] 10314738 51365
..# Dimnames:List of 2
.. ..$ : chr [1:10314738] "line1" "line2" "line3" "line4" ...
.. ..$ : chr [1:51365] "sparito" "davide," "15enne" "di" ...
.. .. ..- attr(*, ".match.hash")=Class 'match.hash' <externalptr>
..# x : num [1:120671481] 1 1 1 1 1 1 1 1 1 1 ...
..# factors : list()
This object comes from the function dtm_builder of text2map package. Since I would like to remove empty rows from the matrix, I thought about using the command:
raw.sum=apply(dtm,1,FUN=sum) #sum by raw each raw of the table
dtm2=dtm[raw.sum!=0,]
Anyway, I obtained the following error:
Error in asMethod(object): Cholmod error 'problem too large' at file ..
How could I fix it?
The short answer to your problem is that you're likely converting a sparse object to a dense object. Matrix package sparse matrix classes are very memory efficient when a matrix has a lot of zeros (like a DTM) by simply not allocating memory for the zeros.
#akrun's answer should work, but there is a rowSums function in base R and a rowSums function from the Matrix package. You would need to load the Matrix package first.
Here is an example dgCMatrix (note not loading Matrix package yet)
m1 <- Matrix::Matrix(1:9, 3, 3, sparse = TRUE)
m1[1, 1:3] <- 0
class(m1)
If we use the base R rowSums you get the error:
rowSums(m1)
Error in rowSums(dtm): 'x' must be an array of at least two dimensions
If the Matrix package is loaded,rowSums will be replaced with the Matrix package's own method, which works with dgCMatrix. This is also true for the bracket operators [. If you update text2map to version 0.1.5, Matrix is loaded by default.
That is a massive DTM, so you may still run into memory issues -- which will depend on your machine. One thing to note is that removing sparse rows/columns will not help much. So, although words that occur once or twice will make up about 60% of your columns, you will reduce the size in terms of memory more by removing the most frequent words (i.e. words with a number in every row).
Error in match.arg(opt_crit) : 'arg' must be NULL or a character vector
occurs when trying to run my script in r.
I have tried to find the solution for it, but it seems to be pretty specific, and little help for me.
My dataset contains 3936 obs of 7 variables.
environment, skill, volume, datetime, year, month, day
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3696 obs. of 7 variables:
$ environment: chr "b2b" "b2b" "b2b" "b2b" ...
$ skill : chr "BO Bedrift" "BO Bedrift" "BO Bedrift" "BO Bedrift" ...
$ year : num 2017 2017 2017 2017 2017 ...
$ month : num 1 1 1 1 1 2 2 2 2 3 ...
$ day : num 2 9 16 23 30 6 13 20 27 6 ...
$ volume : num 360 312 305 222 113 ...
$ datetime : Date, format: "2017-01-02" "2017-01-09" "2017-01-16" "2017-01-23" ...
but when trying to run
volume_ets <- volume_tsbl %>% ETS(volume)
this message shows in the console
Error in match.arg(opt_crit) : 'arg' must be NULL or a character vector
I tried somewhat of a shortcut, but nothing helped,
volume_tsbl$volume <- as.numeric(as.character(volume_tsbl$volume))
Tried to run
volume_ets <- volume_tsbl %>% ETS(volume)
this message shows in the console
Error in match.arg(opt_crit) : 'arg' must be NULL or a character vector
I tried somewhat of a shortcut, but nothing helped,
volume_tsbl$volume <- as.numeric(as.character(volume_tsbl$volume))
volume_ets <- volume_tsbl %>% ETS(volume)
my tsibble looks like this;
volume_tsbl <- volume %>¤ as_tsibble(key = c(skill, environment), index = c(datetime), regular = TRUE )
Expected the code to run, but it does not.
This is the result of an interface change made in late 2018. The change was to make model functions (such as ETS()) create model definitions, rather than fitted models. Essentially, ETS() no longer accepts data as an input, and the specification for the ETS model would become ETS(volume).
The equivalent code in the current version of fable is:
volume_ets <- volume_tsbl %>% model(ETS(volume))
Where the model() function is used to train one or more model definitions (ETS(volume) in this case) to a given dataset.
You can refer to the pkgdown site for fable to see more details: http://fable.tidyverts.org/
In particular, the ETS() function is documented here: http://fable.tidyverts.org/reference/ETS.html
I am working with "rehh" package of R.
I create an object chr21 of class haplohh from data2haplohh function of the package.
Now when I try to write it to a file:
write.table(chr21, file = "CHR21", append = FALSE, quote = TRUE,sep = "\t", eol="\n", na= "NA", dec=".", row.names=TRUE, col.names=TRUE)
The error I get is:
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class "structure("haplohh", package = "rehh")" to a data.frame
Also when I try to print first 10 rows of chr21,
head(chr21, n=10)
I get this error:
Error in x[seq_len(n)] : object of type 'S4' is not subsettable
OK so am adding the output of str(chr21):
str(chr21)
Formal class 'haplohh' [package "rehh"] with 6 slots
..# haplo : num [1:10, 1:1010554] 0 2 2 2 0 2 0 2 0 2 ...
..# position: num [1:1010554] 9411410 9411645 9411785 9412503 9413228 ...
..# snp.name: chr [1:1010554] "rs78200054" "rs71235074" "rs71235075" "rs71220884" ...
..# chr.name: chr "21"
..# nhap : int 10
..# nsnp : int 1010554
I am a newbie in R, It would be really great If I could get to know where I am going wrong and how to fix this error.
Thanks in advance!
library(rehh)
#Copy example files in the current working directory.
make.example.files()
#Chreate some sampel data
chr12<-data2haplohh(hap_file="bta12_hapguess_switch.out",map_file="map.inp",
min_maf=0.05,popsel=7,chr.name=12,recode.allele=TRUE)
# Look at the structure of the object (in your case it is called chr21)
str(chr12)
Formal class 'haplohh' [package "rehh"] with 6 slots
..# haplo : num [1:280, 1:1202] 2 2 1 2 2 2 1 2 2 2 ...
..# position: num [1:1202] 79823 125974 175087 219152 256896 ...
..# snp.name: chr [1:1202] "F1200140" "F1200150" "F1200170" "F1200180" ...
..# chr.name: chr "12"
..# nhap : int 280
..# nsnp : int 1202
You can extract various components from this object:
# Extract data matrix from it
haplo.matrix <- chr12#haplo
# Extract position
pos <- chr12#position
head(pos)
#[1] 79823 125974 175087 219152 256896 316254
If you need to get data back into a dataframe format you can do the following:
df <- data.frame(chr=chr12#chr.name, snp.name=chr12#snp.name, position=chr12#position, stringsAsFactors=FALSE)
df <- cbind(df, t( chr12#haplo))
Once this is done, you can use head() and other regular R functions.
However if you need to apply the functions from rehh package you should use original chr21 object
In R, if I would like to plot the spectrogram from a wave, it is as following:
>library(sound)
>library(tuneR)
>library(seewave)
>s1<-readWave('sample1.wav')
>spectro(s1,main='s1')
>str(s1)
Formal class 'Wave' [package "tuneR"] with 6 slots
..# left : int [1:312000] 2293 2196 1964 1640 1461 1285 996 600 138 -195 ...
..# right : num(0)
..# stereo : logi FALSE
..# samp.rate: int 8000
..# bit : int 16
..# pcm : logi TRUE
But what if I just have data.txt as
2293 2196 1964 1640 1461 1285 996 600 138 -195 ...
What should I put in the spectro function? spectro(wave, f, ...), wave is said to be an R object. Or I should use others to get the plot? I tried
>s_1<-read.table("s_1.txt", sep=" ")
>spectro(s_1,f=8000)
Error in filled.contour.modif2(x = X, y = Y, z = Z, levels = collevels, :
no proper 'z' matrix specified
and ended with error. Thank you.
I agree the documentation is a little hazy.
What I believe it is trying to say is that the first argument must be a Wave object. You can convert a numeric vector into a Wave object using the TuneR Wave() function.
v <- runif(5000, -2^15, 2^15-1)
v.wav <- Wave(v, samp.rate=8000, bit=16)
spectro(v.wav)
I didn't manage to install seewave on my current computer, so I tested this on an old computer with software a couple of years old. I can't guarantee that this example will work.
I've a csv file of daily bars, with just two lines:
"datestamp","Open","High","Low","Close","Volume"
"2012-07-02",79.862,79.9795,79.313,79.509,48455
(That file was an xts that was converted to a data.frame then passed on to write.csv)
I load it with this:
z=read.zoo(file='tmp.csv',sep=',',header=T,format = "%Y-%m-%d")
And it is fine as print(z) shows:
Open High Low Close Volume
2012-07-02 79.862 79.9795 79.313 79.509 48455
But then as.xts(z) gives: Error in coredata.xts(x) : currently unsupported data type
Here is the str(z) output:
‘zoo’ series from 2012-07-02 to 2012-07-02
Data:List of 5
$ : num 79.9
$ : num 80
$ : num 79.3
$ : num 79.5
$ : int 48455
- attr(*, "dim")= int [1:2] 1 5
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "Open" "High" "Low" "Close" ...
Index: Date[1:1], format: "2012-07-02"
I've so far confirmed it is not that 4 columns are num and one column is int, as I still get the error even after removing the Volume column. But, then, what could that error message be talking about?
As Sebastian pointed out in the comments, the problem is in the single row. Specifically the coredata is a list when read.zoo reads a single row, but something else (a matrix?) when there are 2+ rows.
I replaced the call to read.zoo with the following, and it works fine whether 1 or 2+ rows:
d=read.table(fname,sep=',',header=T)
x=as.xts(subset(d,select=-datestamp),order.by=as.Date(d$datestamp))