Warning message: In `...` : invalid factor level, NA generated
The warning message is because your "Type" variable was made a factor and "lunch" was not a defined level. Use the stringsAsFactors = FALSE
flag when making your data frame to force "Type" to be a character.
> fixed <- data.frame("Type" = character(3), "Amount" = numeric(3))
> str(fixed)
'data.frame': 3 obs. of 2 variables:
$ Type : Factor w/ 1 level "": NA 1 1
$ Amount: chr "100" "0" "0"
>
> fixed <- data.frame("Type" = character(3), "Amount" = numeric(3),stringsAsFactors=FALSE)
> fixed[1, ] <- c("lunch", 100)
> str(fixed)
'data.frame': 3 obs. of 2 variables:
$ Type : chr "lunch" "" ""
$ Amount: chr "100" "0" "0"
invalid factor level, NA generated error when trying to link colour information in with subgroup
Instead of doing this one by one, an option is factor
factor(pca_matrix[,"Subgroup"], levels = paste0("C", 1:6),
labels = c("blue", "red", "yellow", "green", "black", "white"))
invalid factor level, NA generated warning in a character column
You have the row and column indices mixed up. To change the value of CropTypeName7 (column 21) for row number 2, use EnvPerak[2,21] <- "Yam"
or EnvPerak[2,"CropTypeName7"] <- "Yam"
, not EnvPerak[21,2] <- "Yam"
.
Invalid Factor Level, NA generated R
You could use ifelse()
on the column, changing all values at once.
gss.s$abany <- ifelse(gss.s$abany == "YES", 1, -1)
gss.s
## abany advfront arrest
## 2 1 Agree NO
## 3 1 Strongly agree YES
## 10 1 Agree YES
## 16 1 Agree NO
## 21 -1 Strongly agree NO
Warning message In `[-.factor`(`*tmp*`, iseq, value = foo) : invalid factor level, NA generated when trying to add vector to row subset
I have reduced your function based on my understanding , let me know if it gives what you want or if I have misunderstood something
addPointsToKeyRow = function(df, keyRowNum, searchString, pointsVector) {
#Find columns which has searchString in it
cols <- grepl(searchString, colnames(df))
#Check if the columns with searchString and length of pointsVector is the same
if (sum(cols) == length(pointsVector)) {
#Assign the value
df[keyRowNum,cols] <- pointsVector
}
#Return the updated dataframe
df
}
#Convert all the variables in the column from factor to character
df[] <- lapply(df, as.character)
#define the values to be replaced
pointVals <- c("2", "2", "2", "2", "2", "2", "2", "1", "1", "1", "1","1", "1")
#Call the function
df <- addPointsToKeyRow(df, 1, "points_q", pointsval)
#Check the dataframe
df
Related Topics
Filter Data.Frame Rows by a Logical Condition
Plotting Two Variables as Lines Using Ggplot2 on the Same Graph
Dictionary Style Replace Multiple Items
Shading a Kernel Density Plot Between Two Points.
Determine Path of the Executing Script
Split Data.Frame Based on Levels of a Factor into New Data.Frames
Add Regression Line Equation and R^2 on Graph
Understanding Exactly When a Data.Table Is a Reference to (Vs a Copy Of) Another Data.Table
Select Rows from a Data Frame Based on Values in a Vector
Overlap Join With Start and End Positions
Use First Row Data as Column Names in R
How to Specifically Order Ggplot2 X Axis Instead of Alphabetical Order
Convert a List to a Data Frame