Linked Questions

8votes
1answer
687views

Automatically generate command to reproduce an object in the workspace [duplicate]

Suppose an object is already defined in the workspace: a <- round( rnorm(10) ) [1] 0 -1 -1 -1 -1 0 2 1 1 1 How can I programatically generate a command which creates a? For example, I ...
user avatar
  • 1,465
0votes
1answer
124views

How to read/import a CSV once and create code so that you don't have to read/import again [duplicate]

I have an R script that I would like to share with people, but it currently depends on reading many separate csv files into data frames in order to run. I’m wondering if there’s any quick and easy way ...
user avatar
  • 65
0votes
1answer
66views

R data.frame command from existing dataframe [duplicate]

is there a way in R to produce the "code" to create a dataframe, based on an existing dataframe? This would make it easier to ask SO questions with reproducible examples. For example: > data(...
user avatar
  • 1,542
0votes
0answers
73views

What is the function to convert dataframe to code? [duplicate]

I for the life of me can neither remember nor find the function that can convert a dataframe in your environment to code that writes a dataframe i.e. for posting online or sharing. Can anyone help?
user avatar
  • 2,387
-4votes
1answer
71views

Filtering a dataframe [duplicate]

I have a main dataframe called main and a subset of this dataframe called main1 . I want to create a dataframe main2 which is includes everything in main which is not main1 . How would I do ...
user avatar
  • 127
0votes
0answers
53views

How to transfer values into reproducible code [duplicate]

I know how to manually create reproducible code, e.g. a<-c(0,1,1,0,0,1) b<-c("A","B","A","B","A","B") c<-c(1,1,2,2,3,3) However, I want to provide the code with my actual data (with >100 ...
user avatar
  • 135
0votes
0answers
38views

data.frame into structure(list()) [duplicate]

How can I transform a data.frame in my workspace into a structure(list())-form like HERE in order to share it with this community. Always been asking this myself. Let's take the built-in dataframe ...
user avatar
  • 9,781
0votes
0answers
38views

Function returning the code to create a dataframe [duplicate]

I am looking for a function to get the code generating a data frame in R. I haven't found any simple function that would allow me to do this. Of course I could hard code something to do it. I was ...
user avatar
  • 660
1vote
0answers
35views

How to share a dataframe in stack overflow [duplicate]

I have lots of questions I need help answering in R, but the only way I know how to share my dataframe is to post a screenshot of it, which isn't user-friendly. Can someone please tell me a more ...
user avatar
0votes
0answers
33views

Problem with dput(head()) in order to make a detailed example [duplicate]

im trying to attach a sample of my dataset with the order dput(head()) but the result i get in the Console point of the R environment might not be good and correct as it appears. I will attach here ...
user avatar
  • 9
0votes
0answers
22views

create copy paste example from dataframe or matrix in r [duplicate]

Quite often when trying to get help people ask for reproducible examples. Sometimes sharing a text file is not possible so I was wondering if there is a function which outputs the copy/paste command ...
user avatar
  • 409
879votes
26answers
1.8mviews

How do I replace NA values with zeros in an R dataframe?

I have a data frame and some columns have NA values. How do I replace these NA values with zeroes?
user avatar
655votes
16answers
1.7mviews

Plot two graphs in same plot in R

I would like to plot y1 and y2 in the same plot. x <- seq(-2, 2, 0.05) y1 <- pnorm(x) y2 <- pnorm(x, 1, 1) plot(x, y1, type = "l", col = "red") plot(x, y2, type = "l", col = "green") But ...
user avatar
411votes
20answers
1.1mviews

How to rename a single column in a data.frame?

I know if I have a data frame with more than 1 column, then I can use colnames(x) <- c("col1","col2") to rename the columns. How to do this if it's just one column? Meaning a ...
user avatar
  • 25.4k
220votes
5answers
40kviews

How to make good reproducible pandas examples

Having spent a decent amount of time watching both the r and pandas tags on SO, the impression that I get is that pandas questions are less likely to contain reproducible data. This is something that ...
user avatar
  • 54k
97votes
7answers
130kviews

How to delete columns that contain ONLY NAs?

I have a data.frame containing some columns with all NA values. How can I delete them from the data.frame? Can I use the function, na.omit(...) specifying some additional arguments?
user avatar
102votes
15answers
129kviews

Error: C stack usage is too close to the limit

I'm attempting to run some fairly deep recursive code in R and it keeps giving me this error: Error: C stack usage is too close to the limit My output from CStack_info() is: Cstack_info() ...
user avatar
  • 1,021
67votes
8answers
185kviews

How to sort a data frame by date

I need to sort a data frame by date in R. The dates are all in the form of "dd/mm/yyyy". The dates are in the 3rd column. The column header is V3. I have seen how to sort a data frame by column ...
user avatar
  • 673
94votes
7answers
158kviews

Understanding the order() function

I'm trying to understand how the order() function works. I was under the impression that it returned a permutation of indices, which when sorted, would sort the original vector. For instance, > ...
user avatar
95votes
9answers
170kviews

break/exit script

I have a program that does some data analysis and is a few hundred lines long. Very early on in the program, I want to do some quality control and if there is not enough data, I want the program to ...
user avatar
  • 1,363
62votes
7answers
219kviews

R - argument is of length zero in if statement

I am having a little problem with R and I am not sure why. It is telling me that this line: if(temp > data[[k]][[k2]]) { is of argument length 0. Here is the block which is not that big: for(k ...
user avatar
117votes
4answers
7kviews

`levels<-`( What sorcery is this?

In an answer to another question, @Marek posted the following solution: https://stackoverflow.com/a/10432263/636656 dat <- structure(list(product = c(11L, 11L, 9L, 9L, 6L, 1L, 11L, 5L, ...
user avatar
92votes
2answers
89kviews

Legend on bottom, two rows wrapped in ggplot2 in r

library(ggplot2) library(scales) library(reshape2) Rdates <- c("2007-01-31","2007-02-28","2007-03-30","2007-04-30","2007-05-31","2007-06-29&...
user avatar
51votes
11answers
8kviews

Learning R. Where does one Start? [closed]

I've been using R for a little over a year now and it's been a successful venture. But all too often, I find that there is something that I can't figure out for lack of knowing how to find it or an ...
user avatar
49votes
5answers
121kviews

assign headers based on existing row in dataframe in R

After transforming a dataframe, I would like to assign heads/names to the columns based on an existing row. My headers are currently: row.names X2 X3 X4 X5 X6 X7 X8 X9 ... I would like ...
user avatar
53votes
8answers
139kviews

Export a list into a CSV or TXT file in R

I understand that we cannot export a table if one of its elements is a list. I got a list in R and I want to export it into a CSV or TXT file. Here is the error message that I get when I execute this ...
user avatar
  • 2,083
29votes
8answers
151kviews

how to remove multiple columns in r dataframe?

I am trying to remove some columns in a dataframe. I want to know why it worked for a single column but not with multible columns e.g. this works album2[,5]<- NULL this doesn't work album2[,c(5:...
user avatar
40votes
6answers
164kviews

Eliminating NAs from a ggplot

Very basic question here as I'm just starting to use R, but I'm trying to create a bar plot of factor counts in ggplot2 and when plotting, get 14 little colored blips representing my actual levels and ...
user avatar
27votes
21answers
82kviews

Error in gzfile(file, "wb"): cannot open the connection or compressed file

I'm trying to run two things: first, I'm creating a PDF with 4x5, ending with dev.off(), and then trying to create a new graph. However, after starting the second plot, I get: Error in gzfile(file, "...
user avatar
78votes
2answers
50kviews

What is difference between dataframe and list in R?

What is difference between dataframe and list in R? Which one should be used when? Which is easier to loop over? Exact problem: I have to first store 3 string elements like "a", "b", "c". Later for ...
user avatar
62votes
2answers
25kviews

tooltip on shiny R?

I want to have a tool-tip in my Shiny R application. Is there any easy way to achieve that? For now, I am creating a density map and I want a simple tool-tip showing "click here to slide through years"...
user avatar
  • 10.8k
47votes
2answers
114kviews

Plot with conditional colors based on values in R [duplicate]

I want to plot a graph with different colors based on values. I wrote the below code, np_graph <- data.frame(C1 = -5:5, C2 = -5:5) x=np_graph2$C1 y=np_graph2$C2 plot(x,y,xlab="PC1",ylab="PC2") ...
user avatar
  • 1,037
43votes
8answers
52kviews

How do I run a high pass or low pass filter on data points in R?

I am a beginner in R and I have tried to find information about the following without finding anything. The green graph in the picture is composed by the red and yellow graphs. But let's say that I ...
user avatar
  • 24.3k
30votes
5answers
106kviews

Categorize numeric variable into group/ bins/ breaks

I am trying to categorize a numeric variable (age) into groups defined by intervals so it will not be continuous. I have this code: data$agegrp(data$age >= 40 & data$age <= 49) <- 3 data$...
user avatar
  • 423
43votes
4answers
40kviews

Sort a factor based on value in one or more other columns

I've looked through a number of posts about ordering factors, but haven't quite found a match for my problem. Unfortunately, my knowledge of R is still pretty rudimentary. I have a subset of an ...
user avatar
  • 813
28votes
10answers
105kviews

Linear model function lm() error: NA/NaN/Inf in foreign function call (arg 1)

Say I have data.frame a I use m.fit <- lm(col2 ~ col3 * col4, na.action = na.exclude) col2 has some NA values, col3 and col4 have values less than 1. I keep getting Error in lm.fit(x, y, ...
user avatar
  • 311
54votes
2answers
518kviews

Non-numeric Argument to Binary Operator Error in R

The issue I believe is how CurrentDay is entered. It was previously created as: Transaction <- function(PnL, Day) results <- list(a = PnL, b = Day) return(results) Both PnL and Day are ...
user avatar
  • 549
54votes
3answers
45kviews

Number formatting axis labels in ggplot2?

I'm plotting a fairly simple chart using ggplot2 0.9.1. x <- rnorm(100, mean=100, sd = 1) * 1000000 y <- rnorm(100, mean=100, sd = 1) * 1000000 df <- data.frame(x,y) p.new <- ggplot(df,...
user avatar
  • 1,860
32votes
4answers
32kviews

How to maintain size of ggplot with long labels

I have a plot that is a simple barplot of number of each type of an event. I need the labels of the plot to be under the plot as some of the events have very long names and were squashing the plot ...
user avatar
31votes
5answers
26kviews

Convert hex to decimal in R

I found out that there is function called .hex.to.dec in the fBasics package. When I do .hex.to.dec(a), it works. I have a data frame with a column samp_column consisting of such values: a373, ...
user avatar
36votes
3answers
25kviews

Error in printing data.frame in excel using XLSX package in R

The dataframe is visible with out any error. But when the same is printed using write.xlsx fucnction of the package XLSX, it gives the error. Error in .jcall(cell, "V", "setCellValue", value) : ...
user avatar
42votes
2answers
94kviews

geom_text how to position the text on bar as I want?

I would like to adjust the text on the barplot. I tried to adjust hjust/vjust to display as I like it but it seems like it's not working properly. ggplot(data) + geom_bar(aes(name, count, ...
user avatar
  • 888
35votes
1answer
102kviews

How to increase the font size of ggtitle in ggplot2

I would like to increase the font size of ggtitle and also the font should be bold. My code is as follows. ggplot(df, aes(x1, y = value, colour = variable)) + geom_point(size=2) + ggtitle("male ...
user avatar
  • 423
45votes
3answers
6kviews

How to create example data set from private data (replacing variable names and levels with uninformative place holders)?

To provide a reproducible example of an approach, a data set must often be provided. Instead of building an example data set, I wish to use some of my own data. However this data can not be released....
user avatar
58votes
1answer
154kviews

import dat file into R

Apologies in advance for the simplicity of this question. I am trying to import a .dat file from a website into R with the following code: www = "http://www.nilu.no/projects/ccc/onlinedata/ozone/...
user avatar
  • 3,830
31votes
5answers
108kviews

Matrix expression causes error "requires numeric/complex matrix/vector arguments"?

ma=diag(3)+t(da)%*%da R Code above, error message as follows: Error in t(da) %*% da : requires numeric/complex matrix/vector arguments da is a matrix, looks as following: V45 V46 ...
user avatar
30votes
6answers
8kviews

Given a set of random numbers drawn from a continuous univariate distribution, find the distribution

Given a set of real numbers drawn from a unknown continuous univariate distribution (let's say is is one of beta, Cauchy, chi-square, exponential, F, gamma, Laplace, log-normal, normal, Pareto, ...
user avatar
  • 47k
10votes
8answers
36kviews

How to show corpus text in R tm package?

I'm completely new in R and tm package, so please excuse my stupid question ;-) How can I show the text of a plain text corpus in R tm package? I've loaded a corpus with 323 plain text files in a ...
user avatar
  • 335
43votes
1answer
66kviews

Figures captions and labels in knitr

I'm new to Knitr. I'm trying to make a report using r chunks, and I can't figure out how to use captions and labels to reference the figure later on. Here's an example of what I would like to do: ---...
user avatar
  • 597
28votes
2answers
57kviews

linear regression "NA" estimate just for last coefficient

I am running the summary(lm(...)) function in R. When I print the coefficients I get estimates for all variables except the last variable. The last variable I get "NA". I tried switching the last ...
user avatar
  • 349

15 30 50 per page
1
2 3 4 5
355