I have a similar table as shown in this example (mine has more variables but the problem remains the same). I need observations taken in the same counties to be given the same index, and stored in J
I need to create the vector J as shown on the right hand side of the picture, can anybody help me with this? The table can be found here. Thanks.
It appears that you would just use your county column?
j = randomdata$county which you can subset as j[919] and get 85.
Related
This question already has answers here:
How to know a dimension of matrix or vector in R?
(6 answers)
Closed 3 years ago.
I know this is probably a very simple question but I can't seem to find the answer anywhere online. I am trying to print just the number of data points inside of a variable that I created but I can't figure out how.
I tried using summary() or num() or n() but I am really just making stuff up here and cannot seem to figure it out at all.
For my specific example I have a data set on peoples heights, age, weight, gender, stuff like that. I used
one_sd_weight <- cdc$weight[abs(cdc$weight - mean(cdc$weight)) <= sd(cdc$weight)]
to determine how many of the weights fall within one standard deviation of the mean. After I do this, I can see that on the right side it created a new variable called one_sd_weight that contains 14152 out of the original 20000 entries. How do I print the number 14152 as a variable? For the work I am doing I need to create a new variable that just contains one number, 14152 or whatever number is produced when I run the code above. For example, I need to create
n_one_sd <- 14152
without typing in 14152, instead typing some function that grabs the number of entries in one_sd_weight.
I have tried things like summary() and n() but only receive error messages in return. Any help is greatly appreciated!!
n_one_sd <- length(one_sd_weight)
You're looking for length (in case of a vector) or nrow in case of a matrix/data.frame.
Or you can use NROW() for both, that should work too.
I am trying to run a loop to change the names of two columns in my data but the name of these two columns start with a number. For the same work I changed them (they were not starting with a number) by writing as shown below but it is not working.
Here is the code (the loop finishes later) :
#Filtering
for(i in 1:length(names$ID)){
f<-names$ID[[i]]
corrpoints<-sprintf("corrpoints%i",as.numeric(levels(f))[f])
pts=readOGR(dsn="C:/Users/Charlie/Desktop/Stage_permafrost/SIG/Quantification_des_mouvements/Corr_points_disp/Corr_points_ubaye", layer=corrpoints)
pts$Gvalue2004<-pts$2004_red_gr
pts$Gvalue2012<-pts$2012_red_gr
pts$Aspect_mnt<-pts$Aspect_25m_
Any idea on how I could fix this?
Thanks
Your example is not reproducible for us.
Column names CAN start with numbers. Use ticks around the name to access it as below. Whether this is a good idea is an entirely different story.
x <- mtcars
colnames(x)[1] <- '1mpg'
x$`1mpg`
I'm wondering if it is possible to change the contents of multiple cells in a table using R?
Consider this example: Example
I need to change the values 'Femini.' to 'Feminine'. The problem is that i have a great number of cells to change... Is there some command that help me doing this?
Thanks for the help,
Luís
Say your dataframe is called df
df$Genre[df$Genre == 'Femini'] <- 'Feminine'
I am trying to do a sumifs in Google Sheets that sums based on a number of variables held in cells. I want to be able to vary the dates in two cells to change the range that is summed. My formula looks like:
=SUMIFS(D2:D500,A2:A500,">8/01/15",A2:A500,"<9/01/15",F2:F500,C1012)
I want to be able to replace the two dates with cells. When I do, I get a formula parse error. I have seen a lot of questions about doing this for formatting, but not in this context.
Can anyone help?
Assuming your dates are in I1 and J1 please try:
=SUMIFS(D2:D500,A2:A500,">"&I1,A2:A500,"<"&J1,F2:F500,C1012)
I think my problem is less difficult than it seems to me right now:
In R, I have a data frame with several columns. Two of them are called "PlotId" and "Landuse":
PlotId Landuse
---------------------
000AEG01 Wiese
000AEG02 Weide
000AEG03 Maehweide
000AEG04 ...
"PlotId" contains 50 rows with values from "000AEG01" to "000AEG50", "Landuse" contains three levels: "Wiese", "Weide" and "Maehweide".
For another problem, which I try to solve, I simply need to get the "Landuse"-value for the corresponding "PlotId"-row. SO, I need a command that gives me the information: "Plot 000AEG01 corresponds to Landuse Wiese." And so on for all the other rows, so I probably need to write a loop for that. This information, I would like to get as an object, which I can use then within another loop. I hope, you get what I mean and can help me!
Thanks a lot in advance!