Spread function fails - r

R newbie here, re-building Python pipeline in R.
d is a data.frame():
$ day (chr) "2016-10-13", ...
$ city_name (chr) "SF", ...
$ type (chr) "Green", ...
$ count (int) 10, ...
I'm doing a spread() on the data from tidyr package:
d %>% spread(type,count)
Works fine running locally (Mac) with:
R version 3.3.0 (2016-05-03)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.5 (El Capitan)
tidyr_0.6.1
I run the identical command on a Linux box, on the same input d, but it returns an error:
Error in `[.data.table`(.variables, lengths != 0) : i evaluates to a logical vector length 3 but there are 930 rows. Recycling of logical i is no longer allowed as it hides more bugs than is worth the rare convenience. Explicitly use rep(...,length=.N) if you really need to recycle.
On Linux, I'm running:
R version 3.2.5 (2016-04-14)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu precise (12.04.5 LTS)
tidyr_0.6.0
Any idea what this error means, and why it would be thrown?
Edit: fixed this after updating tidyr on Linux, and re-starting the R session.

Related

Extract non English characters, except white spaces from author names in R [duplicate]

This question already has answers here:
Convert accented characters into ascii character
(2 answers)
Closed 1 year ago.
I have the following data:
authors <- c("Fernando Carré", "Adrüne Coça", "Pìso Därço")
And I want to extract non-english characters and convert them into ASCII, but without the spaces. This is what I have tried:
gsub("[^[:alnum:]]","",authors)
But it returns:
[1] "FernandoCarré" "AdrüneCoça" "PìsoDärço"
It should return:
"Fernando Carre" "Adrune Coca", "Piso Darco"
Any help will be greatly appreciated.
Thanks for Onyambu correction, the following statement is not correct
The expression [[:alnum:]] is made for the package stringr only. It cannot be used in other packages. Hence we can use
But here is what I got from the console.
> authors <- c("Fernando Carré", "Adrüne Coça", "Pìso Därço")
> iconv(authors ,to="ASCII//TRANSLIT")
[1] "Fernando Carre" "Adrune Coca" "Piso Darco"
> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.1 LTS

Why can't I set the memory.limit in R x64 above 4GB?

I can't increase my memory limit above approx 4GB in R (Windows 10). I have 128 GB of RAM. I've heard there's a bug in R studio, so I went directly into the R console. It's still not working. Ideas?
memory.size()
[1] 4135.24
memory.size(max=TRUE)
[1] 4196.69
memory.limit(size=128000)
[1] 130980
Warning message:
In memory.limit(size = 128000) : cannot decrease memory limit: ignored
And...FYI:
sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)
Thanks!
I realize this question is rather old, but as I don't see an answer to the question...
To increase your memory limit, specify a number greater than the value returned by
memory.limit(max = NA)
for the size argument in memory.limit(), eg,
memory.limit(size = 133120)
memory.limit(size = 128000) returns an error because your system's memory limit is already greater than 128000. Your current limit is 130980, the value returned by memory.limit().
To see your current memory limit, run:
memory.limit(max = NA)
memory.size(max = TRUE) returns a relatively small value because it returns "the maximum amount of memory obtained from the OS," rather than your memory limit (see https://www.rdocumentation.org/packages/utils/versions/3.6.2/topics/memory.size). (Whether that max is in the current session, ever, the current R version... I don't know.)
For comparison, on R with ~8 GB memory limit:
memory.size()
#> [1] 50.7
memory.size(max = TRUE)
#> [1] 52
memory.limit(size = 1)
#> Warning in memory.limit(size = 1): cannot decrease memory limit: ignored
#> [1] 8042
memory.size(max = NA)
#> [1] 8042.05
Created on 2022-03-20 by the reprex package (v2.0.0)

Is there an argument to force UTF8 in igraph functions?

I am trying to use an adjacency matrix that has labels in UTF-8. Is there a way to make sure iGraph functions use UTF8, something along the line of "encoding = "UTF8""? This is to avoid the following result (French text on a japanese system shows kanji instead of french diacritics). Thanks for any pointers.
> m1 <- graph_from_adjacency_matrix(m, mode = "directed", weighted = TRUE)
> m1
> IGRAPH 7a99453 DNW- 391 1454 --
+ attr: name (v/c), weight (e/n)
+ edges from 7a99453 (vertex names):
[1] Accept ->Accepter Acknowledge->Appr馗ier Acknowledge->Confirmer Acknowledge->Conscient
[5] Acknowledge->Consid駻er Acknowledge->Constater Acknowledge->Convenir Acknowledge->Donner
+ ... omitted several edges
As per requested:
R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252
[4] LC_NUMERIC=C LC_TIME=French_France.1252
Actually, this may not be an issue with igraph, but with RStudio, since I just realised that although my tables are properly displayed with the View() command, if I just call them in the console, the French diacritics are displayed with Japanese kanji. In any cases, igraph also does this.

Rstudio and R terminal give different outputs

In Rstudio (using R 3.1.1) when I run this,
length(unique(sort(c(outer(2:100,2:100,"^")))))
# 9220
In R 3.1.1 when I run this,
length(unique(sort(c(outer(2:100,2:100,"^")))))
# 9183
(the correct output is 9183)
I can't figure out why... help is greatly appreciated
As David Arenburg notes, this is a difference between 32-bit and 64-bit R versions, at least on Windows machines. Presumably, some sort of rounding error is involved. Interestingly, it is the 32-bit R gets the answer right, whereas the 64-bit R finds too many unique numbers.
First to confirm that 9183 is indeed the correct answer, I used the gmp package (a wrapper for the C multiple precision arithmetic library GMP), which provides results that are not subject to rounding errors:
library(gmp)
x <- as.bigz(2:100)
length(unique(do.call(c, sapply(x, function(X) x^X))))
[1] 9183
Here are the results from my 32-bit R:
length(unique(sort(c(outer(2:100,2:100,"^")))))
# [1] 9183
R.version[1:7] _
# platform i386-w64-mingw32
# arch i386
# os mingw32
# system i386, mingw32
# status
# major 3
# minor 1.2
And here are the results from my 64-bit R:
length(unique(sort(c(outer(2:100,2:100,"^")))))
# [1] 9220
R.version[1:7]
# platform x86_64-w64-mingw32
# arch x86_64
# os mingw32
# system x86_64, mingw32
# status
# major 3
# minor 1.2

R sprintf dec2hex error

Running R on linux (see version below output below)
I experience weird behavior with sprintf converting dec to hex.
Does anybody know what could explain this? (i.e. first conversion works fine, second returns an error regarding numeric):
> sprintf("%x",2109440182)
[1] "7dbb80b6"
> sprintf("%x",2151028214)
Error in sprintf("%x", 2151028214) :
invalid format '%x'; use format %f, %e, %g or %a for numeric objects
version
_
platform x86_64-pc-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 3
minor 0.1
year 2013
month 05
day 16
svn rev 62743
language R
version.string R version 3.0.1 (2013-05-16)
nickname Good Sport
Thanks, Michael
gcc : format ‘%x’ expects an argument of type ‘unsigned int’, but argument 2 has type ‘long int’.
I guess the number is larger that an unsigned int. Max range in my system is 2147483648,
So this is correct:
printf("%x\n", 2147483647);

Resources