This is my sample dataset:
vector1 <-
data.frame(
"name" = "a",
"age" = 10,
"fruit" = c("orange", "cherry", "apple"),
"count" = c(1, 1, 1),
"tag" = c(1, 1, 2)
)
vector2 <-
data.frame(
"name" = "b",
"age" = 33,
"fruit" = c("apple", "mango"),
"count" = c(1, 1),
"tag" = c(2, 2)
)
vector3 <-
data.frame(
"name" = "c",
"age" = 58,
"fruit" = c("cherry", "apple"),
"count" = c(1, 1),
"tag" = c(1, 1)
)
list <- list(vector1, vector2, vector3)
print(list)
This is my test:
default <- c("cherry",
"orange",
"apple",
"mango")
for (num in 1:length(list)) {
#print(list[[num]])
list[[num]] <- rbind(
list[[num]],
data.frame(
"name" = list[[num]]$name,
"age" = list[[num]]$age,
"fruit" = setdiff(default, list[[num]]$fruit),#add missed value
"count" = 0,
"tag" = 1 #not found solutions
)
)
print(paste0("--------------", num, "--------"))
print(list)
}
#print(list)
I'm trying to find which fruit miss in the data frame and the fruit is based on the value of the tag.For example, in the first data frame, there are tags 1 and 2.If the value of tag 1 does not have the default fruit such as apple and banana, the missed default fruit will be added to 0 to the data frame.The expectation format likes the following:
[[1]]
name age fruit count tag
1 a 10 orange 1 1
2 a 10 cherry 1 1
3 a 10 apple 1 2
4 a 10 mango 0 1
5 a 10 apple 0 1
6 a 10 mango 0 2
7 a 10 orange 0 2
8 a 10 cherry 0 2
When I check the process of the loop, I also find that the first loop adds mango 3 times and I don't find the reason why it cannot add the missed value at one time.The overall output likes the following:
[[1]]
name age fruit count tag
1 a 10 orange 1 1
2 a 10 cherry 1 1
3 a 10 apple 1 2
4 a 10 mango 0 1
5 a 10 mango 0 1
6 a 10 mango 0 1
[[2]]
name age fruit count tag
1 b 33 apple 1 2
2 b 33 mango 1 2
3 b 33 cherry 0 1
4 b 33 orange 0 1
[[3]]
name age fruit count tag
1 c 58 cherry 1 1
2 c 58 apple 1 1
3 c 58 orange 0 1
4 c 58 mango 0 1
Does anyone help me and provides simple methods or other ways? Should I use the sqldf function to add 0 value?Is this a simple way to solve my problems?
Consider base R methods --
lapply
,expand.grid
,transform
,rbind
,aggregate
-- that appends all possible fruit and tag options to each dataframe and keeps the max counts.Output
A solution using dplyr and tidyr. We can use
complete
to expand the data frame and specify the fill values as 0 tocount
.Notice that I changed your list name from
list
tofruit_list
because it is a bad practice to use reserved words in R to name an object. Also notice that when I created the example data frame I setstringsAsFactors = FALSE
because I don't want to create factor columns. Finally, I usedlapply
instead of for-loop to loop through the list elements.DATA
The OP has requested to complete each data.frame in
list
so that all combinations ofdefault
fruit and tags1:2
will appear in the result wherebycount
should be set to0
for the additional rows. Finally, each data.frame should consist at least of 4 x 2 = 8 rows.I want to propose two different approaches:
lapply()
and theCJ()
(cross join) function fromdata.table
to return a list.list
to one large data.table usingrbindlist()
and apply the required transformations on the whole data.table.Using
lapply()
andCJ()
Ordering by
count
andtag
is not required but helps to compare the result with OP's expected output.Creating on large data.table
Instead of a list of data.frames with identical structure we can use one large data.table where the origin of each row can be identified by an id column.
Indeed, th OP has asked other questions ("using lapply function and list in r" and "how to loop the dataframe using sqldf?" where he asked for help in handling a list of data.frames. G. Grothendieck already had suggested to
rbind
the rows together.The
rbindlist()
function has theidcol
parameter which identifies the origin of each row:Note that
df
contains the number of the source data.frame inlist
(or the names of the list elements iflist
is named).Now, we can apply above solution by grouping over
df
: