Print r vector to copy paste into other code. [duplicate] - r

This question already has answers here:
How to print the structure of an R object to the console
(2 answers)
Closed 8 years ago.
I have a vector I'd like to copy paste into code to be able to produce a minimal working example.
Problem is, when I try to print the vector it produces output like
> head(residuals_list)
1 2 3 4 5 6
0.1833777 7.1833777 1.1833777 4.1833777 5.1833777 0.1833777
How do I get r to print c(0.1833777, 7.1833777, ...)?

With dput :
x =runif(5)
dput(x)
c(0.634340619435534, 0.833359521813691, 0.4804580679629, 0.119585362030193,
0.379494784167036)

Try str(residuals_list) to get a string version of the object

Related

Remove part of a string in R [duplicate]

This question already has answers here:
Changing date format in R
(7 answers)
Closed 2 years ago.
I'm new to R and I have tried the search function but couldn't make much sense of what I found.
I have a string like this: 20/08/2016
How can I convert it to 20/08/16
Any help is much appreciated.
Maybe you can try something like below
s <- "20/08/2016"
format(as.Date(s,"%d/%m/%Y"),"%d/%m/%y")
# [1] "20/08/16"
or
gsub("(.*/)\\d{2}(.*)","\\1\\2",s)
# [1] "20/08/16"

need my string clean using regular expression in R programming [duplicate]

This question already has answers here:
Replace single backslash in R
(5 answers)
Closed 4 years ago.
My string is as below.
[{\"period\":\"01-06-2018\",\"count\":5},{\"period\":\"01-07-2018\",\"count\":8},{\"period\":\"01-08-2018\",\"count\":9}]
but I want only (only backslash) to be removed and it should look like below
(using R programming functions)
[{"period":"01-06-2018","count":5},{"period":"01-07-2018","count":8},{"period":"01-08-2018","count":9}]
This should do it:
library(tidyverse)
somestring<-c("[{\"period\":\"01-06-2018\",\"count\":5},{\"period\":\"01-07-2018\",\"count\":8},{\"period\":\"01-08-2018\",\"count\":9}]")
library(jsonlite)
fromJSON(somestring)
This yields:
period count
1 01-06-2018 5
2 01-07-2018 8
3 01-08-2018 9

After using unname, a blank column name header row still is generated in write.csv [duplicate]

This question already has an answer here:
Write a data frame to csv file without column header in R [duplicate]
(1 answer)
Closed 4 years ago.
Hi i am trying to remove a variable name and dsiplay just the values alone. when doing so, i am getting a space above the first value.
Even when i tried to save it as a csv, that too had a space in the place of the variable names. How to avoid the space? Can anyone help?
I just wanted to display from the first value without any spaces in the place of the variable names.
Sample code:
unname(data.frame(cars$speed))
write.csv(unname(data.frame(cars$speed)), 'C:/Users/pdrajama/Downloads/Personal/R/cars.csv')
Console output:
unname(data.frame(cars$speed))
1 4
2 4
3 7
4 7
5 8
6 9
7 10
8 10
You can try this:
write.table(data.frame(cars$speed),"test.txt",col.names=F,quote=F,sep=",")
write.table has already col.names=F to remove the first line of the data. And you can use sep="," to mimic the write.csv output.

Accessing the last 5 objects of a particular column in a dataframe in R [duplicate]

This question already has answers here:
Getting the last n elements of a vector. Is there a better way than using the length() function?
(6 answers)
Closed 5 years ago.
I am working in a dataframe in R and I want to access the last 5 objects of a particular column in a dataframe.How do i go about it??
One option is tail(dataframe$column, 5)

Capture last output as an R object [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to assign the result of the previous expression to a variable in R?
I'm working with R in ESS and just made the stupid mistake of running a long running function without assigning the result to a variable. So, it just printed out the result, a long string of output that looks like:
[[1]]
1 FALSE
[[2]]
1 TRUE
[[3]]
1 TRUE
[[4]]
1 TRUE
Is there any way to coerce this printed output into an R object? Either within R, or using emacs (M-x undo-my-stupid-mistake)?
Maybe this will work:
out <- .Last.value

Resources