Find all corresponding values [duplicate] - r

This question already has answers here:
Get the index of the values of one vector in another?
(3 answers)
Closed 5 years ago.
I like to give all values which are in the dat vector a corresponding value which I define in another vector (new_value). I already managed to do somehow, but I like to have a solution without loop:
dat <- c("a","c","b","a","a","c","a","b","b","a")
old_value <- names(table(dat))
new_value <- 1:length(old_value)
new_dat <- rep(NA, length(dat))
for(z in 1:length(old_value)){
new_dat[dat==old_value[z]] <- c(1:length(new_value))[z]
}
new_dat
I don't want to use additional libraries. Please only base solutions.

We can use match
new_dat <- new_value[match(dat, old_value)]
For the current example, even
match(dat, old_value)
should work

Related

R: Function that uses variable dataframe names from a vector [duplicate]

This question already has answers here:
How to convert certain columns only to numeric?
(4 answers)
Make a list from ls(pattern="") [R]
(1 answer)
Closed 2 years ago.
I have a number of x dataframes (depending on previous operation). The names of the dataframes are stored in a different vector:
> list.industries
[1] "misc" "machinery" "electronics" "drugs" "chemicals"
Now, I want to set every column after the 4th as numeric. As the number of created dataframes and, therefore, the names change, I want to ask, if there is any way to do it automatically.
I tried:
for (i in 1:length(list.industries)) {
paste0(list.industries) <- lapply(paste0(list.industries)[,4:ncol(paste0(list.industries))] , as.numeric)
}
Where the function places automatically the name of the dataframe from the vector list.industries to set it as numeric.
Is there any way, how I can place the name of a dataframe as a variable from a vector?
Thanks!
You can use mget to get data as a named list, turn every columns after 4th as numeric and return the dataframe back.
new_data <- lapply(mget(list.industries), function(x) {
x[, 4:ncol(x)] <- lapply(x[, 4:ncol(x)], as.numeric)
x
})
new_data would have list of dataframes, if you want the changes to be reflected in the orignal dataframe use list2env.
list2env(new_data, .GlobalEnv)
You could use this fragment (untested):
one_df <- function(x) {
dat <- get(x)
for (i in seq(4, ncol(dat))) dat[,i] <- as.numeric(dat[,i])
return(dat)
}
ans <- lapply(list.industries, one_df)
So in short: you are looking for get.

How to loop through a vector of data frame names to print first columns of the df's? [duplicate]

This question already has answers here:
How to extract certain columns from a list of data frames
(3 answers)
Closed 2 years ago.
so x is a vector. i am trying to print the first col of df's name's saved in the vector. so far I have tried the below but they don't seem to work.
x = (c('Ethereum,another Df..., another DF...,'))
for (i in x){
print(i[,1])
}
sapply(toString(Ethereum), function(i) print(i[1]))
You can try this
x <- c('Ethereum','anotherDf',...)
for (i in x){
print(get(i)[,1])
}
You can use mget to get data in a list and using lapply extract the first column of each dataframe in the list.
data <- lapply(mget(x), `[`, 1)
#Use `[[` to get it as vector.
#data <- lapply(mget(x), `[[`, 1)
Similar solution using purrr::map :
data <- purrr::map(mget(x), `[`, 1)

Duplicate line according to 1 column of a dataframe in R [duplicate]

This question already has answers here:
Repeat each row of data.frame the number of times specified in a column
(10 answers)
Closed 4 years ago.
I have a dataframe like this :
enter image description here
I would like to dupplicate each line the number of times indicated in the column "nombreIndividus".
I tried with rep() and each = and/or time = but I can't do it.
Example :
incomeGlobalCopie <- incomeGlobalCopie[rep(1:nrow(incomeGlobalCopie),
each=incomeGlobalCopie$nombreIndividus)]
Can you help me ?
Thanks
Completely unelegant, but it does the trick:
names <- c("lion","tiger","flamengo")
replication <- c(4,5,3)
species <- data.frame(names, replication)
speciesCopy <- data.frame(matrix(ncol=2,nrow=0))
for(i in 1:length(species$names)){
for(j in 1:species$replication[i]){
speciesCopy <- rbind(speciesCopy, species[i,])
}
}
speciesCopy

Complete blanks by last non-blank value in R [duplicate]

This question already has answers here:
Replacing NAs with latest non-NA value
(21 answers)
Closed 4 years ago.
I'm struggling to solve this apparently simple question in R, but no suceess until now.
I have a data.frame with a char variable having some blanks and some non-blank values. I'm trying to complete those blanks with the last non-blank found into the same variable from top-down as in the following example related do variable 'Species' in data.frame 'want' vs 'have'.
If someone could help, I thanks in advance!
set.seed(12346)
foi <- split(iris, iris$Species)
want <- do.call("rbind", lapply(foi, function(x){
x[1:sample(1:10, 1), ]
}))
row.names(want) <- NULL
want$Species <- as.character(want$Species)
have <- want
have$Species[2:10] <- ""
have$Species[12:16] <- ""
have$Species[18:21] <- ""
head(have, 20)
head(want, 20)
A simple for loop assuming the first value is non missing:
for(i in which(have$Species=="")) have$Species[i]=have$Species[i-1]
You could split your variable by block of consecutive blank values and fill each block with the first previous non blank value if speed is an issue and your file is huge.

how paste function working in R? [duplicate]

This question already has an answer here:
multiply multiple column and find sum of each column for multiple values
(1 answer)
Closed 9 years ago.
this is my code
here the paste function works for only two combination.i need the same code in a loop for more than two combinations at the same time.
i<-2
while (i<=10)
{
results<-data.frame()
results<- t(apply(data,1,function(x) combn(x,i,prod)))
comb <- combn(colnames(data),i)
colnames(results) <- apply(comb,i,function(x) paste(x[1],x[2]))
i<-i+1
}
now i get the two combination like
V1V2, V1V3,V1V4,....
now i want
v1v2v3, v1v2v4, ...
in paste function.
comb <- combn(colnames(data),v)
colnames(results) <- apply(comb,2,function(rows) paste0(rows, collapse = ""))
insted of paste use paste0
#henrik and #chargaff

Resources