Say I have 2 dataframes each with two columns 'pic_type' and 'roi' (in reality I have a lot more dataframes, but 2 will work for this example)
a <- setNames(data.frame(matrix(ncol = 2,nrow =6)), c("pic_type","roi"))
b <- setNames(data.frame(matrix(ncol = 2,nrow =6)), c("pic_type","roi"))
In each dataframe 'pic_type' can be one of two string values ('item', 'relation'), 'roi' can be one of three ('object', 'relation', 'pic'). For example (excuse my poor coding)
a$pic_type <- c("item", "item", "item","relation","relation","relation")
a$roi <- c("object", "object", "pic", "object", "relation","relation")
b$pic_type <- c("item", "item", "item","relation","relation","relation")
b$roi <- c("relation", "relation", "object", "pic", "pic","object")
Which gives:
'a'
pic_type roi
item object
item object
item pic
relation object
relation relation
relation relation
'b'
pic_type roi
item relation
item relation
item object
relation pic
relation pic
relation object
And put them in a list
myList <- list(a,b)
Now I want to use lapply to go through each df in the list and create a new column called 'type' that contains one of three values per row ('occupied', 'empty' or 'nil'). These values are based on the following:
If pic_type = "item" & roi = "object", then type = "occupied"
If pic_type = "relation" & roi = "relation", then type = "occupied"
If pic_type = "item" & roi = "relation", then type = "empty"
If pic_type = "relation" & roi = "object", then type = "empty"
Otherwise type = "nil"
For example:
'a'
pic_type roi type
item object occupied
item object occupied
item pic nil
relation object empty
relation relation occupied
relation relation occupied
I have tried the following:
myList <- lapply(myList, function(x) for(row in 1:dim(x)[1]) {
if(as.data.frame(x)[row,1] == "item" && as.data.frame(x)[row,2]=="object") {as.data.frame(x)[row,3] == "occupied"}
else if(as.data.frame(x)[row,1] == "relation" && as.data.frame(x)[row,2]=="relation") {as.data.frame(x)[row,3] == "occupied"}
else if(as.data.frame(x)[row,1] == "item" && as.data.frame(x)[row,2]=="relation") {as.data.frame(x)[row,3] == "empty"}
else if(as.data.frame(x)[row,1] == "relation" && as.data.frame(x)[row,2]=="object") {as.data.frame(x)[row,3] == "empty"}
else {as.data.frame(x)[row,3] == "null"}})
However this throws up the error:
Error in if (as.data.frame(x)[row, 1] == "item" && as.data.frame(x)[row, :
missing value where TRUE/FALSE needed
Can anyone offer a solution? I am aware that with just two dfs it is easier to do it without lapply, but I have many dfs in the actual list and want to apply this function to each one of them.
Thanks in advance!