Create column in R

0

I have a column with 3 types of information: numeric, percentage, and character. I would like to create two new columns from this, one with only the character information being the remaining "NA" cells and another column with only the numeric information getting the character information and the other "NA" information in this new column. Objective is to create two new columns from the Code column. One with the numbers and another 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")

    
asked by anonymous 21.03.2018 / 18:48

1 answer

0

Quite strange this way of saving 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 fetch and remove % , because only then will a numeric column be created.

Next, check if there was any character in the column to create the character column.

As 70 and 50 are percentages, I created the count perc that signals if the number column is a Percentage.

See if this is 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
    
22.11.2018 / 13:40