Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204922

R HELP! CHANGING COMMAS WITH PERIODS

$
0
0

1

Here is my task:

There is some structual issues with this dataset, write a function, named 'convert_number' that will accomplish the following: - change the numbers in a column so that the ',' is a '.' - convert that column to a double

convert_number <- function(data, col) { countries[col] <- as.character(countries[col]) countries[col] <- scan(text=countries[col], dec=",", sep=".") (countries[col] <- as.double()) }

convert_number("countries", "Net.migration") I first did the following:

countries$Net.migration <- sub("^$", "0", countries$Net.migration) in order to change all the blanks to "0"s so that I can switch out the comma, but I realize a question further down in my assignment asks the amount of NAs in a column so I can't have "0"s in those cells. I am guessing there is a better way to do it than scan(text=...)?

I'm a beginner (especially with functions) and I think I am overlooking a simpler way to do this.

Here is a sample: tail(countries, 5)

tail(countries, 5)

I wrote this function:

convert_number <- function(x){
    x <- as.character(x)
    x <- gsub(pattern = ",", replacement = ".",x = x, fixed = TRUE)
    x <- as.numeric(x)
    return(x)
}
data$Coastline..coast.area.ratio <- convert_number(data$Coastline..coast.area.ratio)

   convert_number <- function(x){
    x <- as.character(x)
    x <- gsub(pattern = ",", replacement = ".",x = x, fixed = TRUE)
    x <- as.numeric(x)
    return(x)
}
data$Coastline..coast.area.ratio <- convert_number(data$Coastline..coast.area.ratio)

**IDK WHAT TO PUT IN X!!!!!!!** 

Viewing all articles
Browse latest Browse all 204922

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>