I'm trying to manipulate some data frames using R, and I keep hitting a roadblock. I began with a data frame femaleDF
with three columns: Product Name, Person.1, Person.2.
The data frame had values in rows like:
'E0001', 'John Smith-M', 'Jane Smith-F'
What I want is to separate the genders into two different data frames. I was able to strip out males from the female list using grepl
searches for '-M'
, so now I have something like:
'E0001', NA, 'Jane Smith-F'
But now I want to shift the female values from Person.2 into Person.1 if Person.1 == NA
. Here's what I have so far:
female.strip <- function(femaleDF) {
increment <- 1
repeat {
if(is.na(femaleDF$Person.1[increment])==TRUE) {
femaleDF$Person.1[increment] <- femaleDF$Person.2[increment]
femaleDF$Person.2[increment] <- NA
increment <- increment + 1
} else {increment <- increment + 1}
if(increment > nrow(femaleDF)){
break
}
}
}
femaleDF <- female.strip(femaleDF)
I think I'm going wrong somewhere in the line where I try to reassign values: femaleDF$Person.1[increment] <- femaleDF$Person.2[increment]
I get errors that look like
1: In
[<-.factor
(*tmp*
, increment, value = structure(c(282L, ... :
invalid factor level, NA generated
I'm sure if I knew more about R basics this would be a breeze, but can anyone help me out? Thanks!
edit: not the same as this question. I didn't create the data frame, I imported it with read.csv
.