I want to generate similar lines in python,
It should have number range only number should change.
I mean I want to generate the same line but the number should go on from 2 to 1000.
EXAMPLE:
Jhon has 2 Apples
Jhon has 3 Apples
Jhon has 4 Apples
Jhon has 5 Apples
Jhon has 6 Apples
Jhon has 7 apples
I need a python script to generate like this.
for num in range(2,1001):
print(f"Jhon has {num} Apples")
This code for solution for your problem. In here I using for loop for generate ittaritvely printing line and formating string concept use for change number variable.
Related
I have a dataset with over 400,000 cows. These cows are (unevenly) spreak over 2355 herds. Some herds are only present once in the data, while one herd is even present 2033 times in the data, meaning that 2033 cows belong to this herd. I want to delete herds from my data that occur less than 200 times.
With use of plyr and subset, I can obtain a list of which herds occur less than 200 times, I however can not find out how to apply this selection to the full dataset.
For example, my current data looks a little like:
cow herd
1 1
2 1
3 1
4 2
5 3
6 4
7 4
8 4
With function count() I can obtain the following:
x freq
1 3
2 1
3 1
4 3
Say I want to delete the data belonging to herds that occur less than 3 times, I want my data to look like this eventually:
cow herd
1 1
2 1
3 1
6 4
7 4
8 4
I do know how to tell R to delete data herd by herd, however since, in my real datatset, over 1000 herds occur less then 200 times, it would mean that I would have to type every herd number in my script one by one. I am sure there is an easier and quicker way of asking R to delete data above or below a certain occurence.
I hope my explanation is clear and someone can help me, thanks in advance!
Use n + group_by:
library(dplyr)
your_data %>%
group_by(herd) %>%
filter(n() >= 3)
I need some help conditionally sorting/switching data based on a factor variable.
I'm not sure if it's a typical use case I just can't formulate properly enough for a search engine to show me a solution or if it is that niche but I haven't found anything yet.
I currently have a dataframe like this:
id group a1 a2 a3 a4 b1 b2 b3 b4
1 1 2 6 6 3 4 4 6 4
2 2 5 2 2 2 2 5 2 3
3 1 6 3 3 1 3 6 4 1
4 1 4 8 4 2 7 8 8 9
5 2 3 1 1 4 2 1 1 7
For context this is from a psychological experiment where people went through two variations of a task and the order of those conditions was determined by the experimental group they were assigned to. The columns represent different measurements from different trials and are currently grouped together for the same variable and in chronological order, meaning a1,a2,a3,a4 are essentially the same variable at consecutive time points, same with b1,b2,b3,b4.
I want to split them up for the different conditions so regardless of which group (=which order of tasks) someone went through, data from one condition should come first in the dataframe and columns should still be grouped together for the same variables and in chronological order within that condition. It should essentially look like this:
id group c1a1 c1a2 c2a1 c2a2 c1b1 c1b2 c2b1 c2b2
1 1 2 6 6 3 4 4 6 4
2 2 2 2 5 2 2 3 2 5
3 1 6 3 3 1 3 6 4 1
4 1 4 8 4 2 7 8 8 9
5 2 1 4 3 1 1 7 2 1
So essentially for group 1 everything stays the same since they happened to go through the conditions in the same order that I want to have in the new dataframe while for group 2 values are being switched where the originally second half of values for each variable is put in front of the originally first one.
I hope I formulated the problem in a way, people can understand it.
My real dataset is a bit more complicated it has 180 columns minus id and group so 178.
I have 13 variables some of which were measured over two conditions with 5 trials for each of those and some which have those 5 trials for each of the 2 main condition but which also have 2 adittional measurements for each condition where the order was determined by the same group variable.
(We essentially asked participants to do the task again in two certain ways, which allowed us to see if they were capable of doing them like that if they wanted to under the circumstences of both main conditions).
So there are an adittional 4 columns for some variables which need to be treated seperately. It should look like this when transformed (x and y are the 2 extra tasks where only b was measured once):
id group c1a1 c1a2 c2a1 c2a2 c1b1 c1b2 c1bx c1by c2b1 c2b2 c2bx c2by
1 1 2 6 6 3 4 4 3 7 6 4 4 2
2 2 2 2 5 2 2 3 4 3 2 5 2 2
3 1 6 3 3 1 3 6 2 2 4 1 1 1
4 1 4 8 4 2 7 8 1 1 8 9 5 8
5 2 1 4 3 1 1 7 8 9 2 1 3 4
What I want to say with this is, I need a pretty general solution.
I already tried formulating a function for creation of two seperate datasets for the groups and then merging them by id but got stuck with the automatic creation and naming of columns which I can't seem to wrap my head around. dplyr is currently loaded and used for some other transformations but since I'm not really good with it, I need to ask for your help regarding a solution with or without it. I'm still pretty new to R and this is for my bachelor thesis.
Thanks in advance!
Your question leaves a few things unclear that make this hard to answer, but here is maybe a start that could help, or at least help clarify your problem.
It would really help if you could clarify 2 pieces of info, what types of column rearrangements you need, and how you distinguish what indicates that a row needs to have this transformation.
I'm also wondering if instead of trying to manipulate your data in its current shape, if it not might be more practical to figure out how to change the shape of your data to better represent your data, perhaps using something like pivot_longer(), I don't know how this data will ultimately be used or what the actual values indicate, but it doesn't seem to be very tidy in its current form, and instead having a "longer" table might be more meaningful, but I'll still provide what I think is a solution to your listed problem.
This creates some example data that looks like it reflects yours in the example table.
ID=seq(1:10)
group=sample(1:2,10,replace=T)
Data=matrix(sample(1:10,80,replace=T),nrow=10,ncol=8)
DataFrame=data.frame('ID'=ID,'Group'=group,Data)
You then define the groups of columns that need to be kept together. I can't tell if there is an automated way for you to indicate which columns are grouped, but this might get bulky if done manually. Some more information on what your column names actually are, and how they are distributed in groups would help.
ColumnGroups=list('One'=c('X1','X2'),'Two'=c('X3','X4'),'Three'=c('X5','X6'),'Four'=c('X7','X8'))
You can then figure out which rows need to have rearranged done by using some conditional. Based on your example, I'm assuming when the group variable equals 2, then the rearranging needs to be done, which is what I've used here.
FlipRows=DataFrame$Group==2
You can then have R only apply the rearrangement needed to those rows that need it, and define the rearrangement based on the ordering of the different column groups. I know you ask for a general solution, but is hard to identify the general solution you need without knowing what types of column rearrangements you need. If it is always flipping two sets of consecutive column groups, that would be easier to define without having to type it all out. What I have done here would require you to manually type out the order of the different column groups that you would like the rows to be rearranged as. The SortedDataFrame object seems to be what you are looking for, but might not actually reflect your real data. I removed columns 1 and 2 in this operation since those are ID and group which you don't want overridden.
SortedDataFrame=DataFrame
SortedDataFrame[FlipRows,-c(1,2)]=DataFrame[FlipRows,c(ColumnGroups$Two,ColumnGroups$One,ColumnGroups$Four,ColumnGroups$Three)]
This solution won't work if you need to rearrange each row differently, but it is unclear if that is the case. Try to provide any of the other info requested here, and let me know where this solution doesn't work for you, and that.
I'm new with R and I have the following problem. Maybe it's a really easy question, but I don't know the terms to search for an answer.
My problem:
I have several persons, each person is assigned a studynumber (SN). And each SN has one or more tests being performed, the test can have multiple results.
My data is long at the moment, but I need it to be wide (one row for each SN).
For example:
What I have:
SN testnumbers result
1 1 1234 6
2 1 1234 9
3 2 4567 6
4 3 5678 9
5 3 8790 9
What I want:
SN test1result1 test1result2 test2result1
1 1 6 6 NA
2 2 6 NA NA
3 3 9 NA 9
So I need to renumber the testnumbers into test 1 etc for each SN, in order to use the spread function, I think. But I don't know how.
I did manage to renumber testnumber into a list of 1 till the last unique testnumber, but still the wide dataframe looks awful.
Say I have a number of users who are evenly split between two teams (if there is an odd count then one team will have one extra player).
I want to make sure everyone gets to be on the same team as everyone else over the course of 3 games with team changes after each game.
What's an easy mechanism for doing this for any number of players?
If it makes it easier for the purpose of explanation, I can give each player a number from 1 to N (where N is the number of players).
TIA
Let's assume we have 6 players, 1 - 6. You want to create different teams for 3 rounds of play.
For the first round, you deal the players.
1 2
3 4
5 6
For the second round, you put the winning team first, then the losing team. Let's assume that the team with player 1 won. Then the list of players would look like this.
1 3 5 2 4 6
And you would deal them like this.
1 3
5 2
4 6
For the third round, you do the same as the second round. This time, let's assume the team with player 3 won.
3 2 6 1 5 4
And you would deal them like this.
3 2
6 1
5 4
With only 3 rounds and many more than 6 players, everybody isn't going to be able to play everybody. But this shuffling algorithm gives a good mixture and is relatively simple to implement.
Okay, so I'm really stuck. I have a data set which looks like this:
Species Latitude Longitude Oiling Condition BirdCount Date_ Oil_Cond Date week.number
1 Northern Gannet 30.32860 -89.19810 Not Visibly Oiled Live 1 2010-07-21 1 2010-07-21 30
2 Laughing Gull 30.23172 -88.32127 Not Visibly Oiled Live 1 2010-05-05 1 2010-05-05 19
3 Northern Gannet 30.26677 -87.59248 Visibly Oiled Live 1 2010-05-05 2 2010-05-05 19
4 American White Pelican 29.29649 -89.66432 Not Visibly Oiled Live 1 2010-05-05 1 2010-05-05 19
5 Brown Pelican 29.88244 -88.87624 Visibly Oiled Live 1 2010-05-08 2 2010-05-08 19
6 Brown Pelican 29.00290 -89.36961 Not Visibly Oiled Live 1 2010-05-14 1 2010-05-14 20
7 Northern Gannet 30.33390 -85.56565 Unknown Live 1 2010-05-17 6 2010-05-17 21
8 Common Loon 30.28177 -87.51028 Not Visibly Oiled Live 1 2010-05-17 1 2010-05-17 21
9 Brown Pelican 30.41410 -88.24542 Visibly Oiled Live 1 2010-05-18 2 2010-05-18 21
10 Northern Gannet 30.24063 -88.12451 Not Visibly Oiled Live 1 2010-05-18 1 2010-05-18 21
And I'm trying to get a faceted histogram plotting the variable Oil_Cond for the 5 most frequent species of birds (there are over 100 unique bird species).
At first I wanted to produce a facet with all the species and used the following code:
qplot(Oil_Cond, data = birds, facets = Species ~., geom = "histogram")
But of course, that overloaded and wouldn't work because there would have been over 100 facets. So then I decided that I really only care about the top 5 species anyways, and I worked out what they are and with what frequency they appear (Laughing Gull: 3036, Brown Pelican: 789, Northern Gannet: 546, Royal Tern: 321, Black Skimmer: 258). However, I am at a loss as to how to do that.
Any help would be much appreciated.
Thank you :)
Amy
The easiest thing to do here may be to simply plot a subset of your data. The only potential thing to be careful of is if the species variable is stored as a factor, rather than as strings. First create a subset:
birdsSub <- subset(birds, Species %in% c('Laughing Gull','Brown Pelican',
'Northern Gannet','Royal Tern','Black Skimmer'))
birdsSub$Species <- droplevels(birdsSub$Species)
and then you should be able to pass this data frame to qplot as you have before. The reason for the droplevels is that if that variable is stored as a factor, all the species that no longer appear will 'come along for the ride' as unused factor levels, and you'll just end up with all 100 panels, all but five them being empty.
You could tackle this using the excellent plyr package...
# If you don't already have plyr installed, uncomment the next line:
# install.packages('plyr')
require(plyr)
# First, find out how many of each species you have...
ns=ddply(birds,.(Species),summarise,n=length(Species))
# This will produce a table listing the number of each species you have
# (in the column 'n'). Type 'ns' to see the table.
# We can then rank the species occurrence, to see how important the different
# species are
ns$r = rank(-ns$n) # negative because 'rank' starts with the lowest number.
# have a look at the top 5 species:
subset(ns,r<=5)
# There are a couple of ways to proceed from here. Either we could get the
# top 5 species names from this 'ns' table:
# names=as.character(subset(ns,r>=5)$Species)
# and use joran's method, or we could merge the ns table and the original
# dataset (so that each species has an 'n' and 'r' attribute) and subset the
# data by species number or rank. I prefer the latter, as it allows you to
# flexibly change the species number threshold. i.e.:
birds=merge(birds,ns,by='Species')
# We've now added 'n' and 'r' columns to the birds data, so we can select
# our subset based on either of these columns:
birds.by.r=subset(birds,r<=5) # selects only the top 5 bird species
birds.by.n=subset(birds,r>=100) # selects all species with over 100 occurrences
# Then just plot away!
qplot(Oil_Cond,data=birds.by.r,facets=Species~.,geom='histogram')
# or
qplot(Oil_Cond,data=birds.by.n,facets=Species~.,geom='histogram')