r-faq

How to reshape data from long to wide format

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 04:33:04
问题 I'm having trouble rearranging the following data frame: set.seed(45) dat1 <- data.frame( name = rep(c("firstName", "secondName"), each=4), numbers = rep(1:4, 2), value = rnorm(8) ) dat1 name numbers value 1 firstName 1 0.3407997 2 firstName 2 -0.7033403 3 firstName 3 -0.3795377 4 firstName 4 -0.7460474 5 secondName 1 -0.8981073 6 secondName 2 -0.3347941 7 secondName 3 -0.5013782 8 secondName 4 -0.1745357 I want to reshape it so that each unique "name" variable is a rowname, with the "values"

How to reshape data from long to wide format

谁说我不能喝 提交于 2019-12-24 04:33:02
问题 I'm having trouble rearranging the following data frame: set.seed(45) dat1 <- data.frame( name = rep(c("firstName", "secondName"), each=4), numbers = rep(1:4, 2), value = rnorm(8) ) dat1 name numbers value 1 firstName 1 0.3407997 2 firstName 2 -0.7033403 3 firstName 3 -0.3795377 4 firstName 4 -0.7460474 5 secondName 1 -0.8981073 6 secondName 2 -0.3347941 7 secondName 3 -0.5013782 8 secondName 4 -0.1745357 I want to reshape it so that each unique "name" variable is a rowname, with the "values"

Change R default library path using .libPaths in Rprofile.site fails to work

感情迁移 提交于 2019-12-24 01:27:39
问题 I am running R on Windows, not as an administrator. When I install a package, the following command doesn't work: > install.packages("zoo") Installing package(s) into ‘C:/Program Files/R/R-2.15.2/library’ (as ‘lib’ is unspecified) Warning in install.packages : 'lib = "C:/Program Files/R/R-2.15.2/library"' is not writable To install a package, I have to specify a library location: install.packages("zoo", lib="C:/software/Rpackages") To load a package, I also have to specify the library

How should I deal with “package 'xxx' is not available (for R version x.y.z)” warning?

安稳与你 提交于 2019-12-19 04:55:58
问题 I tried to install a package, using install.packages("foobarbaz") but received the warning Warning message: package 'foobarbaz' is not available (for R version x.y.z) Why doesn't R think that the package is available? See also these questions referring to specific instances of this problem: My package doesn't work for R 2.15.2 package 'Rbbg' is not available (for R version 2.15.2) package is not available (for R version 2.15.2) package doMC NOT available for R version 3.0.0 warning in install

Why can't I get a p-value smaller than 2.2e-16?

痴心易碎 提交于 2019-12-17 23:07:11
问题 I've found this issue with t-tests and chi-squared in R but I assume this issue applies generally to other tests. If I do: a <- 1:10 b <- 100:110 t.test(a,b) I get: t = -64.6472, df = 18.998, p-value < 2.2e-16 . I know from the comments that 2.2e-16 is the value of .Machine$double.eps - the smallest floating point number such that 1 + x != 1 , but of course R can represent numbers much smaller than that. I know also from the R FAQ that R has to round floats to 53 binary digits accuracy: R FAQ

apply() is slow - how to make it faster or what are my alternatives?

不羁岁月 提交于 2019-12-17 22:41:49
问题 I have a quite large data frame, about 10 millions of rows. It has columns x and y , and what I want is to compute hypot <- function(x) {sqrt(x[1]^2 + x[2]^2)} for each row. Using apply it would take a lot of time (about 5 minutes, interpolating from lower sizes) and memory. But it seems to be too much for me, so I've tried different things: compiling the hypot function reduces the time by about 10% using functions from plyr greatly increases the running time. What's the fastest way to do

Read SAS sas7bdat data into R

一笑奈何 提交于 2019-12-17 10:26:33
问题 What options does R have for reading files in the native SAS format, sas7bdat , into R? The NCES Common Core, for example, contains an extensive repository of data files saved in this format. For concreteness, let's focus on trying to read in this file from LEA Universe in 1997-98, which contains education-agency-level demographics for entities in all states beginning A through I. Here's a preview from SAS of the data: What's the simplest way to bring this data in to my R environment? I don't

Multiline Comment Workarounds?

半城伤御伤魂 提交于 2019-12-17 10:14:57
问题 I (sort of) already know the answer to this question. But I figured it is one that gets asked so frequently on the R Users list, that there should be one solid good answer. To the best of my knowledge there is no multiline comment functionality in R. So, does anyone have any good workarounds? While quite a bit of work in R usually involves interactive sessions (which casts doubt on the need for multiline comments), there are times when I've had to send scripts to colleagues and classmates,

Is there anything wrong with using T & F instead of TRUE & FALSE?

时光总嘲笑我的痴心妄想 提交于 2019-12-17 07:51:30
问题 I noticed that using T and F instead of TRUE and FALSE in functions in R gives me the same results. Of course, T and F are more concise, yet, I see TRUE and FALSE being used more often. I was wondering whether there is any difference between the two? Is there anything wrong with using T and F ? 回答1: T and F can be re-defined, but TRUE and FALSE are reserved words and cannot be re-defined. > TRUE <- 1L Error in TRUE <- 1L : invalid (do_set) left-hand side to assignment > FALSE <- 0L Error in

How do I install a package that has been archived from CRAN?

大城市里の小女人 提交于 2019-12-17 07:30:27
问题 I typed the following in the R command line: install.packages("RecordLinkage") I got the following error: Warning in install.packages : package ‘RecordLinkage’ is not available (for R version 3.1.0) However, one of my coworkers did the exact same thing on the exact same version of R (3.1.0) and it worked. In addition, I've managed to install other packages successfully. Any idea why this does not work? Any help would be greatly appreciated. 回答1: The package has been archived, so you will have