R: Looping over vector of strings for function input [duplicate] - r

This question already has answers here:
R ~ Vectorization of a user defined function
(2 answers)
Closed 2 years ago.
I have a vector of inputs in R and a function f.
inputs <- c('input1','input2','input3',...,'input10')
I want to obtain output <- c(f(input1),f(input2),...,f(input10)) without writing this out.
How can I do this in an efficient manner, and without using a for loop? Also, I'd like to do this without referring to the subscript on input (I just numbered it for the sake of this question).
Further, f('input1') does not work while f(input1) does. How can I resolve this?

Hope Vectorize could help
Vectorize(f)(inputs)

Related

Is there a way to tell R to assume you're subsetting from a particular dataframe? [duplicate]

This question already has answers here:
How do I refer to multiple columns in a dataframe expression?
(1 answer)
When to use 'with' function and why is it good?
(2 answers)
Closed 1 year ago.
I'm writing code which subsets from a dataframe a lot e.g. lots of dataframe_name$column_name and it's a pain to read and to write. Is there a way to tell R that I'm referencing dataframe_name so that I can just write column_name for each instance?

Question on replace within the sample function [duplicate]

This question already has answers here:
Using R to simulate a biased 6 sided dice
(3 answers)
What does the replace mean in sample function? [closed]
(2 answers)
Closed 2 years ago.
I just started using R.
I want to simulate a dice roll. Therefore, I set
z <- sample(6, 12, replace = TRUE)
What does the replace in this function mean? And why does it need to be TRUE?
And also is there any suggestion on looking up these little things contained in the function?
Thank you very much for your time and your help!
?sample will give you the documentation for the function. See the Arguments section.
From the docs, replace means "should sampling be with replacement?". When set to TRUE it means it can select the same element multiple times.
you can write on your *console
?replace
and
?sample
It will give you more details

Is there a function in R that reproduce the squeeze function of Matlab? [duplicate]

This question already has an answer here:
R: Efficiently remove singleton dimensions from array
(1 answer)
Closed 5 years ago.
I'm looking for a function in R that reproduce exactly what the squeeze function of Matlab does. Does anybody know it?
(I'd have thought that it would have been incumbent on an asker to explain what squeeze actually does -- drops singleton dimensions.)
See the help on the drop function in R; this also drops singleton dimensions.

What is a Factor in R and why do we need it [duplicate]

This question already has answers here:
Factors in R: more than an annoyance?
(8 answers)
Closed 5 years ago.
I am new to R language and am not able to understand the need to use/have Factors in R and its killing me. I read a lot but still have not found the satisfactory answer. Any help would be much appreciated. Thank you.
It is a way to associate numbers to different values of a categorical variable.
Let's say you have a vector of genders, and you wish to introduce it to some model (lm for example). Then you need to code the values (0/1 perhaps). Here you will use factor on the vector of genders

R operator: <<- [duplicate]

This question already has answers here:
Why is using `<<-` frowned upon and how can I avoid it?
(2 answers)
Closed 8 years ago.
What's the difference between "<-" and "<<-" in R? I have read the help file in R. But I still don't understand their difference. Could any one provide an example? Thanks a lot!
used inside a function, the standard "<-" operator defines a variable inside this function and the variable will not be available outside.
Using "<<-" enables your function to define a variable outside, aka in the global environment, for further use.
That is quite useful in fact!

Resources