In R, find value in one CSV, isolate it in another [closed] - r

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 working on a project in R, regarding baseball. I have two CSV's that I'm working with. One file, CSV2: "PitchingPost.csv" is all postseason pitching stats, and the column I'm looking at there is the "teamID". I'm trying to evaluate regular season pitching stats in another file, CSV1: "pitching.csv" but only for teams that made the postseason. So I'm trying to remove all of the items in the "teamID" of CSV1 EXCEPT for those occur in CSV2 "teamID".
Help?

To keep only the rows from your first file that share an ID with rows in your second file, you could try something like that:
pitch <- read.csv("pitching.csv")
pitch_post <- read.csv("PitchingPost.csv")
pitch <- pitch[pitch$teamID %in% unique(pitch_post$teamID),]

Related

I need a faster way of doing this instead of using for loops in R [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 12 months ago.
Improve this question
So what I'm trying to do is add a column of 0's to this data frame, but if any of the rows has the code "h353" within any of the columns in that row, then I want that row to have a 1 instead of a 0 in the new column. I'm not even sure if the code works as is, but I just know it's going to take forever to run in its current state since the file is pretty large. Any suggestions on how to fix this/make it more efficient?
current code
This should do the job:
dat<-data.frame(x=rep(0,30), y=rep(0,30), z=rep(0,30))
dat[2,2]<-"h353"
dat[15,3]<-"h353"
dat[20,1]<-"h353"
dat$md<-0
for (i in 1:length(dat[1,])) {if (i==1){mdrows<-as.character(dat[,i])=="h353"} else {mdrows<-mdrows|as.character(dat[,i])=="h353"}}
dat$md[mdrows]<-1

How is it possible to add 15 to every figure in a column, in a tibble? [duplicate]

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 1 year ago.
Improve this question
I’m new to R and I’m trying to add 15 to every figure in my dataset for a specific column and was wondering how it’s possible to this. Any help would be much appreciated, thanks.
Asssuming you have a data.frame df with a column col that you want to increase:
df$col <- df$col + 15
No loop required, the fundamental objects in R are vectors.

Is there an R function to run the same filter command on all of my columns? [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 1 year ago.
Improve this question
I have an excel database with around 250 objects (names of different people), and I would like to know if there´s a function to perform the same command on all of my objects, I have been using the function grep() with each individual name, but i would like to obtain the urls for each individual name without having to do it manually, is there an easier way of doing it?
enter image description here
`Alejandro Díaz Domínguez` [grep(".gob.mx", `Alejandro Díaz Domínguez`)]
[1] "http://www.csg.gob.mx
[2] "http://www.csg.gob.mx
[3] "https://sic.gob.mx
If your pattern is ".gob.mx" for all columns and every column has a person, you may want to use lapply().
lapply(your_dataframe, function(x) x[grep(".gob.mx", x)])

I'd like to add an extra column (variable) to my existing data set as part of an assignment but am unsure how to do it [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
In R Studio I have a data set of 10 columns and I am required to add a further column (further variable) which is the average of 2 columns already there.
We have been told to use this formula: Tav = (Tmax+Tmin)/2 to create an extra column for the average of tmax and tmin but it does not work for me.
I attach an image showing my situation:
I have tried to search for a solution on this site and others but cannot seem to find anything that helps my specific situation.
Thanks for any help in advance.
Next time please read these first before posting: https://stackoverflow.com/help/how-to-ask
How to make a great R reproducible example?
Based on your screenshot, I think this is what you're after:
abp$tav <- (abp$tmax+abp$tmin)/2

Why does data get altered while applying a function [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 8 years ago.
Improve this question
I loaded a RDS file. The file contains a numeric field. When I say
class(NEI$Emissions)
it returns
"numeric"
The data is in maximum 3 digits and contains 3 digits of decimal. However, when I issue the command
max(NEI$Emissions)
it returns a huge number.
646952
How can I use the numeric values as it is?
R doesn't lie. One of your data points is not what you expect.
Find which row has the problem with this command:
which.max(NEI$Emissions)
then examine that row of your original data. You will find the errant value.

Resources