I have a character vector of the type a<- c('ES1-5', 'ES14-26', 'ES27-38', 'ES6-13', 'SA1-13', 'SA14-25') and it is a column of a dataframe.
What I would like to do is to transform into a factor with levels 1,2,3,4,5,6 and subsequently transform into a numerical vector of 1,2,3,4,5,6 and colbind into the dataframe.
Could someone give me an elegant way to do this please.
You can use as.numeric(as.factor))
##Some random data##
a<- c('ES1-5', 'ES14-26', 'ES27-38', 'ES6-13', 'SA1-13', 'SA14-25')
x <- tibble(x = rnorm(6),
y = rnorm(6),
a = a) ##Append vector a as column in a dataframe
##Make a into a factor and append to the dataframe
x$a_factor <- as.numeric(factor(x$a))
x
Related
I have a data frame, all values are numeric, I am interested in plotting all the values of the second row against a vector of the same length.
vector = seq(400, 2498, 2)
this vector is length 1050. This is the same length of the row.
I want to plot this row against the values of the vector and join the dots of the plot.
Based on your answer to my question, the following should work.
vector = seq(400, 2498, 2)
# A dummy data frame with two rows.
my.df <- as.data.frame(t(
data.frame(values = runif(1050),
other.values = runif(1050))))
plot(x = vector,
y = my.df[1, ]) # we select row one here. Choose the row number you want.
HTH!
I have two lists, A and B:
List A contains K character vectors of length W. Each vector contains the same W string values but the indices of the strings may differ. We can think of this list in practice as containing vector of variable names, where each vector contains the same variable names but in potentially-differing orders..
List B contains K character vectors of length W. Each vector can contain W arbitrary values. We can think of this list in practice as containing vectors with the corresponding values of the variables contained in each vector of List A.
I am trying to generate a data frame that is K rows long and W rows wide, where the column names are the W unique values in each vector in List A and the values for each row are drawn from the vector found in that row's index in List B.
I've been able to do this (minimal working example below) but it seems very hackish because it basically involves turning the two lists into data frames and then assigning values from one as column names for the other in a loop.
Is there a way to skip the steps of turning each list into a data frame before then using a loop to combine them? Looping through the lists seems inefficient, as does generating the two data frames rather than a single data frame that draws on contents of both lists.
# Declare number of rows and columns
K <- 10
W <- 5
colnames_set <- sample(LETTERS, W)
# Generate example data
# List A: column names
list_a <- vector(mode = "list", length = K)
list_a <- lapply(list_a, function(x) x <- sample(colnames_set, W))
# List B: values
list_b <- vector(mode = "list", length = K)
list_b <- lapply(list_b, function(x) x <- rnorm(n = W))
# Define function to take a vector and turn it into a
# data frame where each element of the vector is
# assigned to its own colun
vec2df <- function(x) {
x %>%
as.data.frame(., stringsAsFactors = FALSE) %>%
t() %>%
as.data.frame(., stringsAsFactors = FALSE)
}
# Convert vectors to data frames
vars <- lapply(list_a, vec2df)
vals <- lapply(list_b, vec2df)
# Combine the data frames into one
# (note the looping)
for(i in 1:K){
colnames(vals[[i]]) <- vars[[i]][1, ]
}
# Combine rows into a single data frame
out <- vals %>%
dplyr::bind_rows()
rownames(out) <- NULL
# Show output
out
Arrange the data in list_b so that the variables are aligned. We can use Map/mapply to do this, convert the output to dataframe and name the columns.
setNames(data.frame(t(mapply(function(x, y) y[order(x)], list_a, list_b))),
sort(colnames_set))
I am trying to write an R program that can create a vector.
Suppose I have 3 factors (X with levels 1,2,3, Y with levels 1,2, and Z with level 1,2,3,4). If I want to represent them in a contingency table there are 3x2x4=24 cells. (for example, (111, 112, 121, 222) are typical cells).
I want to write a for loop that the output is a vector of all cells. that means the output is a vector of length 24.
vector1 <- factor(x = c(1,2,3))
vector2 <- factor(x = c(1,2))
vector3 <- factor(x = c(1,2,3,4,3,2))
df1 <- expand.grid(levels(vector1),levels(vector2),levels(vector3))
results <- paste0(df1$Var1,df1$Var2,df1$Var3)
factor(results)
Here's another solution using lapply :
create dummy data and assign it to a list
set.seed(10)
listofvectors<-list(
factor(sample(1:3,20, replace = TRUE)),
factor(sample(1:2,10, replace = TRUE)),
factor(sample(1:4,15, replace = TRUE))
)
generate a table of combinations
combinations <-expand.grid(lapply(listofvectors, levels))
combine row-wise then create vector
unlist(do.call("paste0", combinations))
I've got a frame with a set of different variables - integers, factors, logicals - and I would like to recode all of the "NAs" as a numeric across the whole dataset while preserving the underlying variable class. For example:
frame <- data.frame("x" = rnorm(10), "y" = rep("A", 10))
frame[6,] <- NA
dat <- as.data.frame(apply(frame,2, function(x) ifelse(is.na(x)== TRUE, -9, x) ))
dat
str(dat)
However, here the integers turn into factors; when I include as.numeric(x) in the apply() function, this introduces errors. Thanks for any and all thoughts on how to deal with this.
apply returns a matrix of type character. as.data.frame turns this into factors by default. Instead, you could do
dat <- as.data.frame(lapply(frame, function(x) ifelse(is.na(x), -9, x) ) )
a<- data.frame(sex=c(1,1,2,2,1,1),bq=factor(c(1,2,1,2,2,2)))
library(Hmisc)
label(a$sex)<-"gender"
label(a$bq)<-"xxx"
str(a)
b<-data.frame(lapply(a, as.character), stringsAsFactors=FALSE)
str(b)
When I covert dataframe a columns to character,the columns labels disappeared.My dataframe have many columns.Here as an example only two columns. How to keep columns labels when numeric convert to character? Thank you!
Labels are not a commonly used R feature. Unfortunately, you will have to do it yourself:
b <- data.frame(lapply(a, function(x) { y <- as.character(x); label(y) <- label(x); y }), stringsAsFactors = FALSE)