Converting my spatial coordinates into a sf object in R [closed] - r

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm quite new to R and currently stuck on the following task. I have spatial data in the following format:
lat long
1 49,6837508756316 8,97846155698244
2 49,9917393661473 8,2382869720459
3 51,308416699361 12,4118696787101
4 50,7048668720388 6,62725165486336
...
and so on. It's a pretty large data set.
I've been advised to convert my data set into sf data to properly work with it. Can somebody help my with that? I think one problem might also be, that the decimal mark is an ,.
Thanks for your help guys!

I assume the data is in a data.frame called sf:
sf <- data.frame(lat=c("49,6837508756316","49,9917393661473","51,308416699361","50,7048668720388"),long=c("8,97846155698244","8,2382869720459","12,4118696787101","6,62725165486336"), stringsAsFactors = FALSE)
The problem is, that the entries are characters, so you have to convert them to numeric. This can be done via as.numeric, but this function expects the decimals to be seperated by a dot ., hence you have to convert the comma to a dot and then call as.numeric. The conversion can be done using the function gsub.
sf$lat <- as.numeric(gsub(",",".",sf$lat))
sf$long <- as.numeric(gsub(",",".",sf$long))
If you have many columns and you dont want to copy-paste the above for every column, I would suggest you to use:
sf[] <- lapply(sf, function(colValues) as.numeric(gsub(",",".",colValues)))

Related

Convert Factors to Numbers But Getting N/As for The Entire Column [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
First off, I have looked at all the examples and previous questions and have not been able to find a usable answer to my situation.
I have a data set of 300ish independent variables I'm trying to bring into R. The variables are all classified as factors. In my csv file I'm uploading, all of the variables are pricing data with two decimal places. I have used the following code and some of the variables have been converted with decimals. However, many of the converted columns are filled with NAs; in fact, some entire columns are completely NAs.
dsl$price = as.numeric(as.factor(dsl$price)) # <- this completely changes the data into something unrecognizablbe
dsl$price = as.numeric(as.character(dsl$price)) # <- lots of NAs or totally NAs
I've tried to recode the variables in the original CSV file to numeric, but with no luck.
Convert the factor into character which can then be converted into numeric
dsl$price <- as.numeric(as.character(dsl$price))

Dataframe convert factors to numerical error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to convert factors from a data-frame to numeric using the commands
data[] <- lapply (data, function(x) as.numeric(as.character(x))
But it keeps asking me for more coding. What am I doing wrong?
The data-frame is named data and it consists of 50 rows and 2 columns. Will this command change every variable in numeric right? Or shall I do something else?
screenshot after using 'dput' at http://imgur.com/Sde9QSk.png
Shouldn't you add ) at the end of your code?

Subset in R producing na [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This is the code I used to look at a subset of data:
active<-clinic[
(clinic$Days.since.injury.physio > 20 & clinic$Days.since.injury.physio < 35)
&(clinic$Days.since.injury.F.U.1 > 27 & clinic$Days.since.injury.F.U.1 < 63)
, ]
I'd like to select a group of subjects based on two criteria and then analyze their results. To start I was looking at the descriptive data when I noticed na's that exceeded the entire data set.
Subsetting seemed to result in NA's. I've looked at several posts including these two below that seem relevant but I don't understand how to apply the answers.
Why does subset cause na's that don't exist in the full data set? (I think the answer from other posts is that there is an na in another variable?)
How do I work around this?
I'd like to be able to get values from the variables that are present rather than ignoring the whole row if there is a missing value.
Thank you.
Subsetting R data frame results in mysterious NA rows
NA when trying to summarize a subset of data (R)
This is a workaround, a response to your #2
Looking at your code, there is a much easier way of subsetting data. Try this.
Check if this solves your issue.
library(dplyr)
active<- clinic %>%
filter(Days.since.injury.physio>20,
Days.since.injury.physio<35,
Days.since.injury.F.U.1>27,
Days.since.injury.F.U.1<63
)
dplyr does wonders when it comes to subsetting and manipulation of data.
The %>% symbol chains statements together so you don't ever have to use the $ symbol.
If, for some bizarre reason, you don't like this, you should look at the subset function in r.

Factors R not fun: pulling labels vs levels [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm loading a csv file that contains colleges and their conferences into R. When I read the file and create a data frame, it automatically makes the conferences class factor. All I want is to pull the conference but I can only pull the "levels" being random numbers. When I use as.character it stores the random numbers. Can anyone assist me with this?
the following issue has taken me so long to make zero progress so I'd greatly appreciate guidance / assistance.
> data <- read.csv("Regression Data Working File.csv",stringsAsFactors = FALSE)
# the file is essentially just a list of colleges in one column and their corresponding conference in the other column
> class(data$conference) # is a vector of college conferences (SEC, ACC, etc.)
[1] "character"
> data$conference[2]
[1] "7" # should be "ACC" and it is "ACC" when I use View(data)
Ok, here's what I did to fix this. My original file had the column of conferences populated using a vlookup but I made sure to copy and paste these results as values (not knowing if the vlookup function instead of the data would impact the data in the csv file / r). In response to the comment above to provide a sample data file, I copied and pasted the values into a new excel file and tried that data in r and it worked. So I went back to my previous data file and deleted the vlookup data array in a different sheet to try to find an explanation and that resolved the issue. So my guess is that something about the conversion from an excel file into a csv file used the data array that was used for the vlookup and stored the values as that. Thanks for your help in troubleshooting this! Have a great weekend
Thanks,
OP

Formatting data in R? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How do I format dates in R? I had to change something in my data set to account for blanks, and now my dates are very large negative numbers. I need to change them back into dates.
Assuming you have \t delimited input file.
Use the as.is argument to stop the read.table() function from converting the input variables. Then perhaps convert the date into something usable using strptime()
data <- read.table(file="...", sep="\t", as.is = TRUE)
data[,1] <- strptime(data[,1], "%Y-%m-%d")

Resources