All Questions
23 questions
0
votes
1
answer
50
views
In R: Naming elements of a vector as, e.g., c(name1=x1,name2=x2) without elements' prior names interfering
When I create vectors using output from previous functions, I like to give the elements name.
For example:
x=rnorm(100)
z=sample(c(0,1),100,replace=TRUE)
y=.5*x+.5*x^2+z+rnorm(100)
linMod=lm(y~x+z)
...
0
votes
0
answers
66
views
R: extract names of partially named vector
I have a vector that is partially named. All values (and names) are distinct.
my_vec <- c('one',
'two' = '2',
'three' = '3')
Sometimes I need the values i.e. unique(my_vec) ...
3
votes
5
answers
126
views
Get vector of column names depending on logicals of same dataframe
I have a named dataframe containig logicals with missings and I want to get a vector with the column names where values are TRUE (going down the rows and, if multiple TRUEs in one row, going from left ...
0
votes
2
answers
294
views
Assigning names to certain ranges of values in vector based on their positions [R]
I have a numeric vector which stores some values, for example:
vec <- sample(200:800, 20, replace = TRUE)
And an example two numeric variables indicating certain positions within this vector:
pos1 ...
1
vote
0
answers
70
views
How can 'names' attribute have the same length of the vector?
full2=glmmTMB(feedp~reprosex+foodav+rainfall+temp+(1|scan)
+(1+reprosex.pregnant+reprosex.earlylac+reprosex.latelac+reprosex.male||ID),
data=t.data, family=beta_family(link=&...
1
vote
1
answer
297
views
How can I order a vector by the order of its attributes in R?
Say I have a vector and its name as follows:
vct <- c(67, "apple", 88, "orange", 71)
names(vct) <- c("c1", "b2", "d66", "a65", &...
0
votes
1
answer
337
views
Change column names of numeric vector?
Take output<-matrix(sample(9),nrow=3,ncol=3). This gives me:
> output
[,1] [,2] [,3]
[1,] 8 2 3
[2,] 4 7 6
[3,] 9 5 1
I want to change the column names to "a", "...
0
votes
0
answers
130
views
How to concatenate names from differents objects using R?
I am a new user in R and now I have a question. I have a numeric object like:
QSW<- as.numeric(c(1:50))
This vector is the input of my function defined as:
HAR_ReCov<-function(vec){
x<-vec
...
3
votes
0
answers
1k
views
Why does R strip names of vector extracted from a one-column matrix with named rows?
I want to take one row of matrix M and treat the row as a named vector, with the column names of the original matrix as the names of the vector. Usually M[x, ] does what I want but this fails if:
(a) ...
1
vote
1
answer
992
views
Get variables names from a data frame in a vector
I can obtain the names of my variables of a data frame with the command "names (df)". It gives say:
#[1] "V1" "V2" "V3"
But I would like to obtain a vector, something like this:
c("V1", "V2", "V3"...
2
votes
1
answer
82
views
R: Sorting vectors by mean in decreasing order while printing the vector names
This is a very basic question. For an assignment, I need to compare the volumes of two bottle-filling machines. Essentially, I want to know which of the two machines fills to a greater volume, so I'm ...
2
votes
2
answers
371
views
Speeding up naming vector in Rcpp
I started to use Rcpp and be able to speed up R-code incredibly. However changing names of vector elements (like 'v.attr("names")=X' or 'v.names()=X') is very slow in my hand. Is there any solutions ...
4
votes
2
answers
1k
views
convert named vector to list but keep vector names
I have a named vector that I want to convert to a list, as such:
a = 1:10
names(a) = letters[1:10]
as.list(a)
$a
[1] 1
$b
[1] 2
$c
[1] 3
Here, the names of each vector is now the name of the list, ...
-1
votes
2
answers
41
views
Matlab vector with names not numbers
Ho can I make a vector like this M = ['Peter', 'Maria', 'Michelle' etc.] and pick a random name from that vector?? I want to make a lottery basically.
2
votes
2
answers
2k
views
Return a variable name after applying a function on a vector in R
Let's say for example my project has numerous objects which I've placed into a vector.
foo <- 10
bar <- 9
pleb <- 4
eian <- 8
pizzaParlor <- c(foo, bar, pleb, eian)
By creating the ...
0
votes
3
answers
305
views
In C++ how can I make a new object for every element in a vector?
Suppose I've got this class:
class dog
{
public:
//dogstuff
private:
//secret dogstuff
};
and then I've got a function that searches through something unrelated and gets me ...
-1
votes
1
answer
2k
views
names function in R not working as expected [duplicate]
I have a vector named v as follows:
> v
Moe Larry Curly Harry Tevez Kagiso Harry Sierra
10 20 30 40 50 60 70 NA 110 NA 1110 1210
Now, I am trying ...
23
votes
2
answers
2k
views
Preserve names when coercing vector from binary to `as.numeric`?
In R, when you coerce a vector from binary to numeric, the names are stripped away.
There are a few possible solutions, which I've outlined before. It seems dangerous to rely on implicit conversion ...
0
votes
1
answer
165
views
Vector names error
Im trying to set names for a vector using the function names, but R gives me an error. I want to create a vector with function and then set name for each number in that vector. I want to do all this ...
14
votes
1
answer
2k
views
Adding two vectors by names
I have two named vectors
v1 <- 1:4
v2 <- 3:5
names(v1) <- c("a", "b", "c", "d")
names(v2) <- c("c", "e", "d")
I want to add them up by the names, i.e. the expected result is
> v3
a ...
-4
votes
1
answer
68
views
Column names for a vector using R?
> coefficients
[1] -0.052993570 0.071073199 -0.004234659 -0.005725687
How can I rename the columns of my vector "coefficients" in order to get?
> coefficients
int X1 ...
3
votes
1
answer
228
views
obtain the position of elements of Y that a are in X in R
In R,
I have a vector of 5 unique elements:
X<-c("A","B","C","D","E")
And a vector of repeated elements:
Y<- c("A","C","M","Z","B","C","C","R","V","D","D","B","A","V","E","E")
I want to ...
3
votes
2
answers
6k
views
How add rownames with no dimensions in R
> Cases <- c(4,46,98,115,88,34)
> Cases
[1] 4 46 98 115 88 34
> str(Cases)
num [1:6] 4 46 98 115 88 34
I want to name row as "total.cases" and I got error attempt to set rownames ...