问题
The data file that I get from an experiment looks like this
VP Selbst ES eigForm eigES andForm andES MM MMRT Wunschform Geschlecht Muttersprache Alter
99 1 unhöflich NA NA NA NA NA NA
99 5 gesellig NA NA NA NA NA NA
99 6 ehrlich NA NA NA NA NA NA
99 NA 5 gründlich NA NA NA NA NA
99 NA 6 treu NA NA NA NA NA
99 NA 5 romantisch NA NA NA NA NA
99 NA NA 6 stark NA NA NA NA
99 NA NA 3 klein NA NA NA NA
99 NA NA 5 rational NA NA NA NA
99 NA ordentlich NA NA ["y"] [0.4] NA NA NA NA
99 NA sentimental NA NA ["n"] [0.5] NA NA NA NA
99 NA warm NA NA ["n"] [0.4] NA NA NA NA
99 NA NA NA 1 2 1 24
So kind of like stairs. The only column that is filled from bottom to top is the first one with the participant id. The others are arranged in order how they appeared in the experiment. The colors are in a random order in the experiment. ratingS and color belong together as well as ratingOw are the ratings for the colors presented in the routine Ow and same for ratingOt and colorOt
What I want is to get rid of these NAs (empty fields) and get something like this instead:
VP Selbst ES eigForm eigES andForm andES MM MMRT Wunschform Geschlecht Muttersprache Alter
99 1 unhöflich 5 gründlich 6 stark NA NA NA NA
99 5 gesellig 6 treu 3 klein NA NA NA NA
99 6 ehrlich 5 romantisch 5 rational NA NA NA NA
99 NA ordentlich NA NA ["y"] [0.4] NA NA NA NA
99 NA sentimental NA NA ["n"] [0.5] NA NA NA NA
99 NA warm NA NA ["n"] [0.4] NA NA NA NA
99 NA NA NA 1 2 1 24
or even:
VP Selbst ES eigForm eigES andForm andES ES MM MMRT Wunschform Geschlecht Muttersprache Alter
99 1 unhöflich 5 gründlich 6 stark ordentlich ["y"] [0.4] 1 2 1 24
99 5 gesellig 6 treu 3 klein sentimental["n"] [0.5] 1 2 1 24
99 6 ehrlich 5 romantisch 5 rational warm ["n"] [0.4] 1 2 1 24
Note: the empty fields are empty in my data as well (no "NA"). Can this be done with reshape? Any solution is appreciated Maybe, @Onyambu can you help?
dput(datnew)
structure(list(VP = c(99L, 99L, 99L, 99L, 99L, 99L, 99L, 99L,
99L, 99L, 99L, 99L, 99L), Selbst = c(1L, 5L, 6L, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA), ES = structure(c(85L, 31L, 19L,
1L, 1L, 1L, 1L, 1L, 1L, 60L, 72L, 90L, 1L), .Label = c("", "abhängig",
"aggressiv", "aktiv", "albern", "ängstlich", "arrogant", "attraktiv",
"belastbar", "besorgt", "clever", "dankbar", "dick", "direkt",
"dominant", "dünn", "egoistisch", "ehrgeizig", "ehrlich", "eigensinnig",
"eitel", "emotional", "empfindlich", "faul", "flexibel", "freundlich",
"furchtlos", "geduldig", "genau", "gerecht", "gesellig", "gierig",
"glücklich", "grob", "groß", "gründlich", "hektisch", "herzlich",
"hilflos", "hilfsbereit", "intelligent", "kalt", "klein", "kleinlich",
"konsequent", "kreativ", "kritisch", "langweilig", "laut", "lieb",
"loyal", "lustig", "modern ", "nachlässig", "nervös", "nett",
"neugierig", "objektiv", "offen", "ordentlich", "passiv", "primitiv",
"rational", "reizbar", "roh", "romantisch", "sachlich", "schwach",
"selbstbewusst", "selbstlos", "sensibel", "sentimental", "sicher",
"sinnlich", "sozial", "sparsam", "sportlich", "stark", "stur",
"tolerant", "träge", "treu", "umsichtig", "undiszipliniert",
"unhöflich", "unnahbar", "verletzlich", "vielseitig", "vorsichtig",
"warm", "weinerlich", "zielstrebig"), class = "factor"), eigForm = c(NA,
NA, NA, 5L, 6L, 5L, NA, NA, NA, NA, NA, NA, NA), eigES = structure(c(1L,
1L, 1L, 36L, 82L, 66L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("",
"abhängig", "aggressiv", "aktiv", "albern", "ängstlich", "arrogant",
"attraktiv", "belastbar", "besorgt", "clever", "dankbar", "dick",
"direkt", "dominant", "dünn", "egoistisch", "ehrgeizig", "ehrlich",
"eigensinnig", "eitel", "emotional", "empfindlich", "faul", "flexibel",
"freundlich", "furchtlos", "geduldig", "genau", "gerecht", "gesellig",
"gierig", "glücklich", "grob", "groß", "gründlich", "hektisch",
"herzlich", "hilflos", "hilfsbereit", "intelligent", "kalt",
"klein", "kleinlich", "konsequent", "kreativ", "kritisch", "langweilig",
"laut", "lieb", "loyal", "lustig", "modern ", "nachlässig",
"nervös", "nett", "neugierig", "objektiv", "offen", "ordentlich",
"passiv", "primitiv", "rational", "reizbar", "roh", "romantisch",
"sachlich", "schwach", "selbstbewusst", "selbstlos", "sensibel",
"sentimental", "sicher", "sinnlich", "sozial", "sparsam", "sportlich",
"stark", "stur", "tolerant", "träge", "treu", "umsichtig", "undiszipliniert",
"unhöflich", "unnahbar", "verletzlich", "vielseitig", "vorsichtig",
"warm", "weinerlich", "zielstrebig"), class = "factor"), andForm = c(NA,
NA, NA, NA, NA, NA, 6L, 3L, 5L, NA, NA, NA, NA), andES = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 78L, 43L, 63L, 1L, 1L, 1L, 1L), .Label = c("",
"abhängig", "aggressiv", "aktiv", "albern", "ängstlich", "arrogant",
"attraktiv", "belastbar", "besorgt", "clever", "dankbar", "dick",
"direkt", "dominant", "dünn", "egoistisch", "ehrgeizig", "ehrlich",
"eigensinnig", "eitel", "emotional", "empfindlich", "faul", "flexibel",
"freundlich", "furchtlos", "geduldig", "genau", "gerecht", "gesellig",
"gierig", "glücklich", "grob", "groß", "gründlich", "hektisch",
"herzlich", "hilflos", "hilfsbereit", "intelligent", "kalt",
"klein", "kleinlich", "konsequent", "kreativ", "kritisch", "langweilig",
"laut", "lieb", "loyal", "lustig", "modern ", "nachlässig",
"nervös", "nett", "neugierig", "objektiv", "offen", "ordentlich",
"passiv", "primitiv", "rational", "reizbar", "roh", "romantisch",
"sachlich", "schwach", "selbstbewusst", "selbstlos", "sensibel",
"sentimental", "sicher", "sinnlich", "sozial", "sparsam", "sportlich",
"stark", "stur", "tolerant", "träge", "treu", "umsichtig", "undiszipliniert",
"unhöflich", "unnahbar", "verletzlich", "vielseitig", "vorsichtig",
"warm", "weinerlich", "zielstrebig"), class = "factor"), MM = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 1L), .Label = c("",
"[\"n\"]", "[\"z\"]"), class = "factor"), MMRT = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 14L, 2L, 1L), .Label = c("",
"[0.42799997329711914]", "[0.4609997272491455]", "[0.46399974822998047]",
"[0.46699976921081543]", "[0.49199986457824707]", "[0.49899983406066895]",
"[0.4999997615814209]", "[0.5069997310638428]", "[0.5079998970031738]",
"[0.5160000324249268]", "[0.5179998874664307]", "[0.5189998149871826]",
"[0.5239999294281006]", "[0.5249998569488525]", "[0.5269997119903564]",
"[0.5319998264312744]", "[0.5339996814727783]", "[0.5349998474121094]",
"[0.5360000133514404]", "[0.5459997653961182]", "[0.557999849319458]",
"[0.5649998188018799]", "[0.5659997463226318]", "[0.5669999122619629]",
"[0.5689997673034668]", "[0.5729999542236328]", "[0.5739998817443848]",
"[0.5749998092651367]", "[0.5779998302459717]", "[0.5829999446868896]",
"[0.5839998722076416]", "[0.5919997692108154]", "[0.5969998836517334]",
"[0.6079998016357422]", "[0.6099996566772461]", "[0.6099998950958252]",
"[0.6109998226165771]", "[0.6159999370574951]", "[0.618999719619751]",
"[0.622999906539917]", "[0.6269998550415039]", "[0.6309998035430908]",
"[0.6399998664855957]", "[0.6419999599456787]", "[0.6509997844696045]",
"[0.6529998779296875]", "[0.6549997329711914]", "[0.6569998264312744]",
"[0.6579999923706055]", "[0.6589999198913574]", "[0.6669998168945312]",
"[0.6749997138977051]", "[0.6809999942779541]", "[0.6840000152587891]",
"[0.6899998188018799]", "[0.6940000057220459]", "[0.6999998092651367]",
"[0.7019996643066406]", "[0.7079999446868896]", "[0.7159998416900635]",
"[0.7179999351501465]", "[0.7209997177124023]", "[0.7239999771118164]",
"[0.7339999675750732]", "[0.7639997005462646]", "[0.7679998874664307]",
"[0.7739999294281006]", "[0.7769997119903564]", "[0.7779998779296875]",
"[0.7819998264312744]", "[0.7909998893737793]", "[0.7949998378753662]",
"[0.7979998588562012]", "[0.7999999523162842]", "[0.8169999122619629]",
"[0.8220000267028809]", "[0.8339998722076416]", "[0.8619997501373291]",
"[0.931999683380127]", "[1.001999855041504]", "[1.0429999828338623]",
"[1.1759998798370361]", "[1.2819998264312744]"), class = "factor"),
Wunschform = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 1L), Geschlecht = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 2L), Muttersprache = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 1L), Alter = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 24L)), row.names = c(21L, 22L,
23L, 112L, 113L, 114L, 203L, 204L, 205L, 304L, 305L, 306L, 395L
), class = "data.frame")
I don't think that this is what you wanted. I typed it in R and copied. Is that right?
回答1:
df[] = lapply(df, function(x) x[order(is.na(x))])
will put all the NA
s at the bottom, while preserving the order of everything else.
We can then either use na.omit()
on the ordered data to get your short result, or omit the rows where everything is NA
for the long result.
# replace "" with NA
datnew[] = lapply(datnew, function(x) {x[x == ""] <- NA; return(x)})
# order each column
datnew[] <- lapply(datnew, function(x) x[order(is.na(x))])
# short result - remove rows with any NA values
na.omit(datnew)
# long result - remove rows that are all NA, skipping the first column
datnew[!apply(is.na(datnew[-1]), 1, all), ]
来源:https://stackoverflow.com/questions/62178006/delete-nas-to-move-data-up-no-stairs-anymore