[R] Simple loop problem

David L Carlson dcarlson at tamu.edu
Mon Oct 31 20:28:23 CET 2016

```This looks like homework and r-help has a policy of not providing answers for homework. But first you need to research what happens when you convert a data frame to a matrix.

Also in your loop, i is a numeric value between 1 and the length of x:

What is x (what class)?
What is the length of an object of that class?
What is the value of class(i) regardless of the value of i?
What is the difference between class "character" and class "factor"?

If you can answer these questions, it will be obvious what is wrong with your code. They are all available online in basic introductions to R.
-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Yahya Laraki
Sent: Monday, October 31, 2016 9:45 AM
To: r-help at r-project.org
Subject: [R] Simple loop problem

Hi everybody,

I’m new to R and i’m trying to learn fundamentals. I’m facing a small problem for which i can’t find a solution online.

What i want to do: write a function to lower case for all the columns in my data.frame if they respect a condition (class = factor)

This code works, but for all my columns : change_lower = function(x) {data.frame(tolower(as.matrix(x)))}

I need more something like this, but it doesn’t work: function (x) { for (i in 1:length(x)) {
if (class(i)=="factor") {
data.frame(tolower(as.matrix(x)))
}
}}

Thank you

Yahya
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help