multiple-columns

Turning one row into multiple rows in r [duplicate]

随声附和 提交于 2021-02-19 02:44:09
问题 This question already has answers here : Combine Multiple Columns Into Tidy Data [duplicate] (3 answers) Closed 3 years ago . In R, I have data where each person has multiple session dates, and the scores on some tests, but this is all in one row. I would like to change it so I have multiple rows with the persons info, but only one of the session dates and corresponding test scores, and do this for every person. Also, each person may have completed different number of sessions. Ex: ID Name

Renaming the column names of pandas dataframe is not working as expected - python

 ̄綄美尐妖づ 提交于 2021-02-18 06:43:28
问题 I am having below pandas dataframe df . I am trying to rename the column names but it not working as expected. Code: mapping = {df.columns[0]:'Date', df.columns[1]: 'A', df.columns[2]:'B', df.columns[3]: 'C',df.columns[4]:'D', df.columns[5]: 'E',df.columns[6]:'F', df.columns[7]: 'G',df.columns[8]:'H', df.columns[9]: 'J'} df.rename(columns=mapping) Output of df.columns : MultiIndex(levels=[['A Index', 'B Index', 'C Index', 'D Index', 'E Index', 'F Index', 'G Index', 'H Index', 'I Index', 'J

Renaming the column names of pandas dataframe is not working as expected - python

让人想犯罪 __ 提交于 2021-02-18 06:43:11
问题 I am having below pandas dataframe df . I am trying to rename the column names but it not working as expected. Code: mapping = {df.columns[0]:'Date', df.columns[1]: 'A', df.columns[2]:'B', df.columns[3]: 'C',df.columns[4]:'D', df.columns[5]: 'E',df.columns[6]:'F', df.columns[7]: 'G',df.columns[8]:'H', df.columns[9]: 'J'} df.rename(columns=mapping) Output of df.columns : MultiIndex(levels=[['A Index', 'B Index', 'C Index', 'D Index', 'E Index', 'F Index', 'G Index', 'H Index', 'I Index', 'J

Why won't pipe operator let me combine successive mutations?

心已入冬 提交于 2021-02-11 12:44:47
问题 I want to mutate columns from this... ...into... When I did the following... villastats<-villastats%>% mutate(HG = ifelse(HomeTeam == "Aston Villa", villastats$FTHG, ifelse(HomeTeam != "Aston Villa", 0, 0))) villastats<-villastats%>% mutate(AG = ifelse(AwayTeam == "Aston Villa", villastats$FTAG, ifelse(AwayTeam != "Aston Villa", 0, 0))) villastats<-villastats%>% mutate(THG=cumsum(villastats$HG)) villastats<-villastats%>% mutate(TAG=cumsum(villastats$AG)) villastats<-villastats%>% mutate(Tot

Long to wide format with several duplicates. Circumvent with unique combo of columns

徘徊边缘 提交于 2021-02-10 19:53:24
问题 I have a dataset similar to this (real one is way bigger). It is in long format and I need to change it to wide format with one row per id. My problem is that there are a lot of different combinations of time, drug, unit and admin. Only a combination of time, drug, unit and admin will be unique and should only occur once pr id. I could not find a solution to this. I would like R to create unique combinations of columns so the data can be transformed to wide format. I have tried melt.data

Define multiple values as missing in a data frame

落爺英雄遲暮 提交于 2021-02-09 15:02:53
问题 How do I define multiple values as missing in a data frame in R? Consider a data frame where two values, "888" and "999", represent missing data: df <- data.frame(age=c(50,30,27,888),insomnia=c("yes","no","no",999)) df[df==888] <- NA df[df==999] <- NA This solution takes one line of code per value representing missing data. Do you have a more simple solution for situations where the number of values representing missing data is high? 回答1: Here are three solutions: # 1. Data set df <- data

R data frame table set the values into columns

喜夏-厌秋 提交于 2021-02-08 10:11:45
问题 There is a table: class<-c("A","B", "B","C","D","D","D") value<-c(1,3,2,5,6,2,5) df<-cbind(class,value) For example, the classes "B" and "C" have more than one value. I want to set every value of the class into a separate column. I would like to get the following output: Could You please help me? Thanks in advance, Best regards, Inna 回答1: This might help: First of all, make it a dataframe using data.frame() class <- c("A","B", "B","C","D","D","D") value <- c(1,3,2,5,6,2,5) df <- data.frame

R data frame table set the values into columns

跟風遠走 提交于 2021-02-08 10:10:15
问题 There is a table: class<-c("A","B", "B","C","D","D","D") value<-c(1,3,2,5,6,2,5) df<-cbind(class,value) For example, the classes "B" and "C" have more than one value. I want to set every value of the class into a separate column. I would like to get the following output: Could You please help me? Thanks in advance, Best regards, Inna 回答1: This might help: First of all, make it a dataframe using data.frame() class <- c("A","B", "B","C","D","D","D") value <- c(1,3,2,5,6,2,5) df <- data.frame

Creating new column based on multiple possible cell possibilities across several columns

人盡茶涼 提交于 2021-02-08 08:25:50
问题 data[, allkneePR := Reduce(`|`, lapply(.SD, `==`, "0082")), .SDcols=PR1:PR3] Hey, I'm trying to look for different diagnoses c("0082", "0083", "0084") across a range of rows and columns in data.table (the dataset is huge). If one of the values is "0082" or "0083" or "0084" in any of the columns PR1:PR3 I want another column that indicates true. Right now this works with the above code, but I am trying to add in multiple diagnoses, not just "0082". I tried the any() function which doesn't work

Creating new column based on multiple possible cell possibilities across several columns

不想你离开。 提交于 2021-02-08 08:25:35
问题 data[, allkneePR := Reduce(`|`, lapply(.SD, `==`, "0082")), .SDcols=PR1:PR3] Hey, I'm trying to look for different diagnoses c("0082", "0083", "0084") across a range of rows and columns in data.table (the dataset is huge). If one of the values is "0082" or "0083" or "0084" in any of the columns PR1:PR3 I want another column that indicates true. Right now this works with the above code, but I am trying to add in multiple diagnoses, not just "0082". I tried the any() function which doesn't work