Thank you so much! Sorry I would have liked to mark multiple solutions but wasn’t sure how to do it. If you have a moment could you clarify how the zip and ... commands are working here?
When I add the splat ... operator after the Cols argument in the function declaration does that turn that argument in the function into an array? So when I input Cols... into funnynt it would be in the form [col1,col2.col3]
Also I am not sure what I am getting wrong here. But when I try
df = DataFrame(id=rand(1:3,10), A=rand(11:13,10),
B=rand(101:103,10), C = rand(25:27,10), D = rand(32:34,10))
10×5 DataFrame
Row │ id A B C D
│ Int64 Int64 Int64 Int64 Int64
─────┼───────────────────────────────────
1 │ 1 12 102 26 32
2 │ 1 13 101 25 34
3 │ 2 13 102 26 33
4 │ 3 11 102 27 33
5 │ 1 13 102 26 33
6 │ 3 11 101 25 32
7 │ 1 11 103 26 34
8 │ 2 12 101 27 34
9 │ 2 12 102 25 32
10 │ 2 12 101 25 32
gdf = groupby(df,:id)
GroupedDataFrame with 3 groups based on key: id
First Group (4 rows): id = 1
Row │ id A B C D
│ Int64 Int64 Int64 Int64 Int64
─────┼───────────────────────────────────
1 │ 1 12 102 26 32
2 │ 1 13 101 25 34
3 │ 1 13 102 26 33
4 │ 1 11 103 26 34
⋮
Last Group (2 rows): id = 3
Row │ id A B C D
│ Int64 Int64 Int64 Int64 Int64
─────┼───────────────────────────────────
1 │ 3 11 102 27 33
2 │ 3 11 101 25 32
function funnynt(id,Cols...; p1=1,p2=2,p3=3)
m= Matrix{Float64}(undef, length(id),length(Cols))
for i in eachindex(Cols)
if p3>0
m[:,i]= (id .+p1).*(Cols[i].+p2*p3)
else
m[:,i]= (id .+p1).*(Cols[i].+p2)
end
end
ncols=Symbol.("id_",names(df,Not(:id)))
return (;zip(ncols,eachcol(m))...)
end
funnynt (generic function with 1 method)
transform(gdf,Cols(:) => funnynt => AsTable)
I get
ERROR: NamedTuple names and field types must have matching lengths
Again I apologize if I am overlooking something elementary, but thanks so much for your time and help.