Create column in R

Asked

Viewed 442 times

0

I have a column with three types of information: numerical, percentage and character. I would like to create two new columns from this, one with only the information in character getting the other cells "NA" and another column with only the numerical information getting the character information and the other "NA" in this new column. Goal is to create two new columns from the Code column. One with the numbers and the other with the codes (factor).

dput(head(estudo, 10)) structure(list(Potreiro = structure(c(3L, 3L, 3L, 3L, 3L, 4L, 3L, 4L, 3L, 4L), .Label = c("1A", "6B", "7A", "7B"), class = "factor"), Code = structure(c(4L, 1L, 8L, 3L, 2L, 4L, 6L, 5L, 8L, 7L ), .Label = c("2", "3", "4", "5", "50%", "70%", "ac", "ad", "av", "cd", "de", "Dem"), class = "factor")), .Names = c("Potreiro", "Code"), row.names = c(NA, 10L), class = "data.frame")

  • 4

    Welcome to Stackoverflow Brasil! Unfortunately, this question cannot be reproduced by anyone trying to answer it. Please, take a look at this link and see how to ask a reproducible question in R. So, people who wish to help you will be able to do this in the best possible way.

  • Be clearer please give examples of how you have tried to resolve the issue and use the formatting tools to improve the readability of the question.

  • Each column of a dataframe has only a guy information. As you are saying, the figures were transformed into character and then, almost certainly, into factor. Please edit the question with the output of dput(df) for us to see how it is.

  • Exactly Rui Barradas, are all as factor but I need to use some as number so I want to create more columns each with a type of information (number, factor...). I tried ifelse(), but gave error: behaviour2$heigth <- ifelse(behaviour2$Code== ">1", Sep = """) Error in ifelse(behaviour2$Code == ">1", Sep = ""): unused argument (Sep = """) and also behaviour2$heigth <- ifelse(behaviour2$Code== ">1") Error in ifelse(behaviour2$Code == ">1") : argument "no" is Missing, with no default Thank you very much!!!

  • 1

    read the link that @Marcusnunes recommended and put the result of dput(head(behaviour2, 10)). A lot of people want to help you, but they can’t guess your problem, your description wasn’t enough.

  • Goal is to create two new columns from the Code column. One with the numbers and the other with the codes (factor). dput(head(study, 10)) Structure(list(Potreiro = Structure(c(3L, 3L, 3L, 3L, 3L, 4L, 3L, 4L, 3L, 4L), label = c("1A", "6B", "7A", "7B"), class = "factor"), Code = Structure(c(4L, 1L, 8L, 3L, 2L, 4L, 6L, 5L, 8L, 7L&Xa; ), label = c("2", "3", "4", "5", "50%", "70%", "ac", "ad", "av", "cd", "de", "Dem"), class = "factor")), names = c("Potreiro", "Code"), Row.Names = c(NA, 10L), class = "data.frame") Thank you very much!!

Show 1 more comment

1 answer

0

Very strange this way of storing the database, but anyway, it follows code that does what you need:

df <- structure(list(Potreiro = structure(c(3L, 3L, 3L, 3L, 3L, 4L, 3L, 4L, 3L, 4L), .Label = c("1A", "6B", "7A", "7B"), class = "factor"), Code = structure(c(4L, 1L, 8L, 3L, 2L, 4L, 6L, 5L, 8L, 7L ), .Label = c("2", "3", "4", "5", "50%", "70%", "ac", "ad", "av", "cd", "de", "Dem"), class = "factor")), .Names = c("Potreiro", "Code"), row.names = c(NA, 10L), class = "data.frame")
df$Code <- as.character(df$Code)
df$numero <- as.numeric(gsub("%", "", df$Code))
df$char <- ifelse(grepl("[[:alpha:]]", df$Code), df$Code, NA)
df$perc <- factor(grepl("%", df$Code), labels = c("Não", "Sim"))

Note that it was necessary to use regular expression to first search and remove the %, for only in this way can a numerical column be created.

Then look for any character in the column to create the character column.

Like the 70 and 50 are percentages, I created the perc signalling if column number is a Percentage.

See if that’s what you needed:

   Potreiro Code numero char perc
1        7A    5      5 <NA>  Não
2        7A    2      2 <NA>  Não
3        7A   ad     NA   ad  Não
4        7A    4      4 <NA>  Não
5        7A    3      3 <NA>  Não
6        7B    5      5 <NA>  Não
7        7A  70%     70 <NA>  Sim
8        7B  50%     50 <NA>  Sim
9        7A   ad     NA   ad  Não
10       7B   ac     NA   ac  Não

Browser other questions tagged

You are not signed in. Login or sign up in order to post.