DEV Community

CoderLegion
CoderLegion

Posted on β€’ Edited on β€’ Originally published at kodblems.com

1

error in fun(newx[, i], ...) : invalid 'type' (character) of argument

πŸŽ‰ Before you dive into this article...

πŸš€ Check out our vibrant new community at CoderLegion.com!

πŸ’‘ Share your knowledge, connect with like-minded developers, and grow together.

πŸ‘‰ Click here to join now!

Problem:
Hi, I want someones' help me on this " error in fun(newx[, i], ...) : invalid 'type' (character) of argument"

Thanks in advance

Solution:
In this case, this error is coming from sum(), since you are trying to sum the character elements in the color column.

sum("a")

Error in sum("a") : invalid 'type' (character) of argument

You require to remove the color column from the x argument, because it is not being employed in aggregation, however is really the by argument.

aggregate(csv[-1], csv["color"], sum)

color val2 val3

1 blue 6 13

2 green 7 3

3 red 11 9

However, you can try this formula method. It can solve your problem.

aggregate(. ~ color, csv, sum)
You can also apply dplyr package for example-

library(dplyr)
csv %>% group_by(color) %>% summarise_each(funs(sum))
color val2 val3
(chr) (int) (int)
1 blue 6 13
2 green 7 3
3 red 11 9
Now, you are able to solve this issue

Thnks

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly β€” using the tools and languages you already love!

Learn More

Top comments (0)

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one serverβ€”search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

πŸ‘‹ Kindness is contagious

If this post resonated with you, feel free to hit ❀️ or leave a quick comment to share your thoughts!

Okay