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.