This question already has answers here:
How to tell what is in one vector and not another?
(6 answers)
Closed 2 years ago.
I'm quite new to R, but I'm trying to make a "randomizer" of sorts.
I have a vector
names <- c('Name1', 'Name2', [...], 'Name13')
I then sample 6 names from the vector to another vector
name_sample_1 <- sample(names, 6)
What i want is to then update the "names" vector by a line of code, and not have to do it manually. I tried running:
names <- names - name_sample_1
But this returned the error 'non-numeric argument to binary operator'. Any ideas on how to do this effectively?
you have to use the handy %in% operator!
names <- paste0("name", 1:20)
sample_names <- sample(names,6)
names_updated <- names[!names %in% sample_names]
Related
This question already has answers here:
How to sort a character vector where elements contain letters and numbers?
(6 answers)
Sort columns numerically in R [duplicate]
(2 answers)
Closed 9 months ago.
I have 100 files, each named "ABC - Day - 1(to 100).csv".
When I read them into R, it is ordered like this: Day1, Day10, Day100, etc. (see figure 1). I know R does this because it is sorting it by character, not by number. Is there a way that I could reorder the path in numerically correct order (Day1, Day2, Day3, ...) without me actually having to manually change my raw file names?
Here is what I have so far:
filenames <- list.files(path="../STEP_ONE/Test_raw",
pattern="ADD_Day+.*sav",
full.names = TRUE) # Reads in path of the 100 files
Let’s suppose you have a vector v with the names of your file (according to what you said, ___Day__.sav). You can subtract the number of the day and reorder the names with the following code:
# Load library
library(stringr)
# Matrix with your files' names and the day
tab <- as.data.frame(str_match(v, "Day\\s*(.*?)\\s*.sav"))
# Column names
colnames(tab) <- c("file.name", "day")
# Day as numeric
tab$day <- as.numeric(tab$day)
# Reorder `tab` according to $day
tab <- tab[order(tab$day),]
This question already has answers here:
Concatenate a vector of strings/character
(8 answers)
Closed last year.
I am trying to rename rows in my dataset. I need to change them like this: first row would be named "IL1", second "IL2",..., "ILn", where n is a number of rows in the dataset.
I know how to change it by for example rownames(df) <- c("IL1","IL2","IL3","IL4"). But type it word by word is possible only in smaller datasets. I need to change it in dataset where are hundreds of rows.
Any ideas? Thank you.
You can use paste0:
rownames(df) <- paste0("IL", 1:nrow(df))
This question already has answers here:
How to create a sequence starting with a character and then with numbers in R
(1 answer)
Make sequential numeric column names prefixed with a letter
(3 answers)
Closed 1 year ago.
Suppose I want to create a column name in R called L1, L2, ..., up to L200. How could I do this for a data frame?
I tried colnames(df) <- c('L1':'L200'), but this does not work (returns error message NAs introduced by coercion), even though there are 200 columns.
Help on this appreciated!
We can use paste
colnames(df) <- paste0("L", 1:200)
or to make it more automatic
colnames(df) <- paste0("L", seq_along(df))
NOTE: The range (:) operator works for integer, and not with character in base R i.e. 'L1' is a string, while 1 is integer, so 1:200 gives the range of values from 1 to 200
Here is another solution:
colnames(df) <- sprintf("L%d", 1:200)
This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 2 years ago.
I have a dataset called df, which has columns a and b with three integers each. I want to write a function for the mean (obviously this already exists; I want to write a larger function and this appears to be where problems are occurring). However, this function returns NA:
mean_function <- function(x) {
mean(df$x)
}
mean_function(a) returns NA, while mean(df$a) returns 2. Is there something I'm missing about how R functions handle datasets, or another problem?
We need [[ instead of $ as it will literally check for x as column and pass a string
mean_function <- function(x) {mean(df[[x]])}
mean_function("a")
If we need to pass unquoted column name, substitute and convert to character with deparse
mean_function<- function(x) {
x <- deparse(substitute(x))
mean(df[[x]]
}
mean_function(a)
This question already has answers here:
Using grep in R to delete rows from a data.frame
(5 answers)
Closed 8 years ago.
I want to find rows in a dataframe that do not match a pattern.
Key = c(1,2,3,4,5)
Code = c("X348","I605","B777","I609","F123")
df1 <- data.frame(Key, Code)
I can find items beginning with I60 using:
df2 <- subset (df1, grepl("^I60", df1$Code))
But I want to be able to find all the other rows (that is, those NOT beginning with I60). The invert argument does not work with grepl. grep on its own does not find all rows, nor can it pass the results to the subset command. Grateful for help.
You could use the [ operator and do
df1[!grepl("I60", Code),]
(Suggested clarification from #Hugh:) Another way would be
df1[!grepl("I60",df1$Code),]
Here is the reference manual on array indexing, which is done with [:
http://cran.r-project.org/doc/manuals/R-intro.html#Array-indexing
Also, you can try this:
Key = c(1,2,3,4,5)
Code = c("X348","I605","B777","I609","F123")
df1 <- data.frame(Key, Code)
toRemove<-grep("^I60", df1$Code)
df2 <- df1[-toRemove,]