问题
I have a dataframe that looks like below:
CustID item sales
1 a1 40
1 a2 40
1 a3 34
1 a4 42
1 a5 21
1 a6 22
2 a1 33
2 a2 30
2 a3 21
2 a4 11
2 a5 19
2 a6 20
I need to create 2 transposed data sets (they should be CSVs) such that each one contains item in groups of 3 each.....
OUTPUT:
csv1 (item 1-3)
CustID itema1 itema2 itema3
1 40 40 34
2 33 30 21
csv2 (item 4-6)
CustID itema4 itema5 itema6
1 42 21 22
2 11 19 20
Please help me out here
回答1:
Not an one-liner solution, but it does what you want.
First using a combination of split
and dcast
you can create and transpose the df.
> items <- split(df, df$item)
>
> library(reshape2)
> dcast(do.call(rbind, items[1:3]), CustID~item )
CustID a1 a2 a3
1 1 40 40 34
2 2 33 30 21
> dcast(do.call(rbind, items[4:6]), CustID~item )
CustID a4 a5 a6
1 1 42 21 22
2 2 11 19 20
Then using write.csv
you can create and save the data.frames into a csv file.
Here's all in one shot:
lapply(seq(1, length(items), 3), function(i){
DF <- dcast(do.call(rbind, items[i:(i+2)]), CustID~item )
write.csv(DF, file = paste0("DF", i, ".csv"))
})
回答2:
Here is another approach, using xtabs
from base R:
x <- xtabs(sales ~ CustID + item, mydf)
x
# item
# CustID a1 a2 a3 a4 a5 a6
# 1 40 40 34 42 21 22
# 2 33 30 21 11 19 20
^^ That first step transposes the data for you.
fileout <- list(A = 1:3, B = 4:6)
Here, we have created a named list of the columns you want to export for each CSV. The names of the list items would be used for the CSV filenames.
Below is an example of how you can use lapply
to write your CSV files.
lapply(seq_along(fileout), function(y)
write.csv(x[, fileout[[y]]],
file = paste(names(fileout[y]), ".csv", sep = "")))
来源:https://stackoverflow.com/questions/19155457/create-multiple-csv-files-based-on-transpose