In R, what does a negative index do?
This is covered in section 2.7 of the manual: http://cran.r-project.org/doc/manuals/R-intro.html#Index-vectors
It is a negative index into the cnt2
object specifying all rows and all columns except the first column.
What does negative index mean in a data frame?
Minus means remove the element with that index, just like "no minus" extract that element with the index
R: Why doesn't the negative index work to create the complement set?
You can just shuffle the indices (sampling without replacement) and then get the first few for testing and the others for training.
indices <- sample(seq(20))
test <- indices[1:10]
train <- indices[11:20]
train
#> [1] 10 8 12 1 7 20 13 18 4 11
test
#> [1] 19 3 15 2 6 9 16 14 17 5
Created on 2021-09-09 by the reprex package (v2.0.0)
Interpretation of negative index when subsetting a data.frame
In the context [, -7]
it means drop the 7
th column from the data frame longley
(or take all columns but the 7
th from longley
).
This is R 101 and you'd do well to read some introductory material. For example, this is covered very early on in the An Introduction to R manual that comes with R or is accessible from the R website. Or you could read ?Extract
.
Here is an example
> head(longley)
GNP.deflator GNP Unemployed Armed.Forces Population Year Employed
1947 83.0 234.289 235.6 159.0 107.608 1947 60.323
1948 88.5 259.426 232.5 145.6 108.632 1948 61.122
1949 88.2 258.054 368.2 161.6 109.773 1949 60.171
1950 89.5 284.599 335.1 165.0 110.929 1950 61.187
1951 96.2 328.975 209.9 309.9 112.075 1951 63.221
1952 98.1 346.999 193.2 359.4 113.270 1952 63.639
> names(longley)
[1] "GNP.deflator" "GNP" "Unemployed" "Armed.Forces" "Population"
[6] "Year" "Employed"
> names(longley)[7]
[1] "Employed"
> head(longley[, -7])
GNP.deflator GNP Unemployed Armed.Forces Population Year
1947 83.0 234.289 235.6 159.0 107.608 1947
1948 88.5 259.426 232.5 145.6 108.632 1948
1949 88.2 258.054 368.2 161.6 109.773 1949
1950 89.5 284.599 335.1 165.0 110.929 1950
1951 96.2 328.975 209.9 309.9 112.075 1951
1952 98.1 346.999 193.2 359.4 113.270 1952
Select one data point per row using indexing vector with negative values
Maybe this is another naive approach. We loop over every row in the matrix and remove index specified in b
.
t(sapply(seq_len(nrow(a)), function(x) a[x, -b[x]]))
# [,1] [,2]
#[1,] 4 7
#[2,] 2 5
#[3,] 6 9
Or using mapply
with split
t(mapply(`[`, split(a, seq_len(nrow(a))), -b))
Is for loop indexing possible with negative values
R in principle is designed to avoid loops where ever possible. In addition to that there are some syntax errors in your code...
If you want to loop along a vector you need to use i in seq_along(x)
not i in x
. A solution to your problem would be indexing the vector directly
for (i in seq_along(x)){
nums[i] <- sum(x < x[i])
}
Edit: The reason you only looped over the positive values is that i in x
starts indexing by -10 wich works for comparing x against i but not to asignment your start asigning at nums[-10]
which is not really suitable...
Related Topics
Finding Overlap in Ranges with R
Creating (And Accessing) a Sparse Matrix with Na Default Entries
Specifying Ggplot2 Panel Width
How to Append a Whole Dataframe to a CSV in R
Operations on Multiple Tables/Datasets with Edit Queries and R in Power Bi
How to Add a Table to a Ggplot
Correctly Specifying "Logical Conditions" (In R)
Split Time Series Data into Time Intervals (Say an Hour) and Then Plot the Count
R Command Line Passing a Filename to Script in Arguments (Windows)
Merge Nearest Date, and Related Variables from a Another Dataframe by Group
How to Conditionally Highlight Points in Ggplot2 Facet Plots - Mapping Color to Column
How to Strip Dollar Signs ($) from Data/ Escape Special Characters in R
Heatmap-Like Plot, But for Categorical Variables