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
Related
This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 3 years ago.
That's probably a duplicate, but I couldn't find the answer anywhere...
Anyways, here's my simple problem. I am loading a yaml file in R with users metadata. After parsing the yaml with yaml::yaml.load_file, the list looks like this:
$users
$users$`1`
$users$`1`$user
[1] "Alice"
$users$`2`
$users$`2`$user
[1] "Bob"
I can get the user with id = 1 by:
user_list$users$`1`$user
That returns:
[1] "Alice"
My question is: how can I transform a numeric 1 into this "quoted" version, so I can pass the id as a function argument that retrieves the user I want?
Instead of using $, use [[
id <- 1
user_list$users[[as.character(id)]]$user
This question already has answers here:
Evaluate expression given as a string
(8 answers)
Closed 5 years ago.
This seems likely to have a simple solution but I am having a hard time getting it. How do I convert this string to numeric?
> a <- "1:10"
Desired solution should be
> 1:10
[1] 1 2 3 4 5 6 7 8 9 10
I have tried as.numeric() (doesn't work), strsplit ":" and getting the end points 1 and 10 (can work but seems clumsy) but is there some simpler way? Thanks.
You can use eval() and parse()
eval(parse(text ="1:10"))
This question already has an answer here:
Naming list elements in R
(1 answer)
Closed 5 years ago.
i tried Naming the Vector in R.
a<-1:5
names(a[2])<-"e" #this is not showing any warning or error but Naming is not done
but
names(a)[2]<-"e" # this is is Naming the Element properly.
Kindly help with Explanation.
The basic difference is in understanding what a[1] and names(a)[1] stands for.
a<-1:5
(This assigns values 1 to 5 and creates a vector)
a[1] # This gives below output i.e the value stored at first location
[1] 1
names(a)[1] # Shows the label associated with the value in this case 'NULL' yet
NULL
Now assigning the required name to the value
names(a)[2]<-"e"
This does the correct assignment and is how the R expects the code. You can then extract the value by element name namely
a["e"] #will give output
2
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
This question already has answers here:
How can I write a comma between each value of a vector?
(2 answers)
Closed 9 years ago.
b = c(1,1,1,1)
[1] 1 1 1 1
What can I do to change b into "1,1,1,1"
Use paste0 setting collapse=","
> paste0(b, collapse=",")
[1] "1,1,1,1"
Note that the result is not longer numeric, it's character.
cat is another alternative
> cat(b, sep=",")
1,1,1,1
as pointed out by #digEmAll, you can not store the output of cat since it only prints the output on the console, if you want to capture that output to store it in an object, then one alternative is using capture.output as in:
capture.output (cat(b, sep=","))
which is the same result provided by paste0 above.